combinators

Shorter way to write this code

流过昼夜 提交于 2019-12-09 02:20:17
问题 The following pattern appears very frequently in Haskell code. Is there a shorter way to write it? if pred x then Just x else Nothing 回答1: You're looking for mfilter in Control.Monad: mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a -- mfilter odd (Just 1) == Just 1 -- mfilter odd (Just 2) == Nothing Note that if the condition doesn't depend on the content of the MonadPlus , you can write instead: "foo" <$ guard (odd 3) -- Just "foo" "foo" <$ guard (odd 4) -- Nothing 回答2: Hm... You are

Cleave a run-time computed value?

元气小坏坏 提交于 2019-12-07 11:19:27
问题 Cleave is a really useful combinator for minimising code duplication. Suppose I want to classify Abundant, Perfect, Deficient numbers: USING: arrays assocs combinators formatting io kernel math math.order math.primes.factors math.ranges sequences ; IN: adp CONSTANT: ADP { "deficient" "perfect" "abundant" } : proper-divisors ( n -- seq ) dup zero? [ drop { } ] [ divisors dup length 1 - head ] if ; : adp-classify ( n -- a/d/p ) dup proper-divisors sum <=> { +lt+ +eq+ +gt+ } ADP zip H{ } assoc

fixed point combinator in lisp

纵饮孤独 提交于 2019-12-07 09:10:57
问题 ;; compute the max of a list of integers (define Y (lambda (w) ((lambda (f) (f f)) (lambda (f) (w (lambda (x) ((f f) x))))))) ((Y (lambda (max) (lambda (l) (cond ((null? l) -1) ((> (car l) (max (cdr l))) (car l)) (else (max (cdr l))))))) '(1 2 3 4 5)) I wish to understand this construction. Can somebody give a clear and simple explanation for this code? For example, supposing that I forget the formula of Y. How can I remember it , and reproduce it long after I work with it ? 回答1: Here's some

Can clojure evaluate a chain of mixed arity functions and return a partial function if needed?

拥有回忆 提交于 2019-12-07 05:04:12
问题 Suppose you have three functions of arity 1, 2 and 3 as below: (defn I [x] x) (defn K [x y] x) (defn S [x y z] (x z (y z))) Does clojure have an evaluation function or idiom for evaluating: (I K S I I) as (I (K (S (I (I))))) returning a parital function of arity 2? I am considering creating a macro that can take the simple function definitions above and expand them to multi-arity functions that can return partial results. I would not want to create the macro if there is already a built in or

The type signature of a combinator does not match the type signature of its equivalent Lambda function

ⅰ亾dé卋堺 提交于 2019-12-07 04:17:48
问题 Consider this combinator: S (S K) Apply it to the arguments X Y: S (S K) X Y It contracts to: X Y I converted S (S K) to the corresponding Lambda terms and got this result: (\x y -> x y) I used the Haskell WinGHCi tool to get the type signature of (\x y -> x y) and it returned: (t1 -> t) -> t1 -> t That makes sense to me. Next, I used WinGHCi to get the type signature of s (s k) and it returned: ((t -> t1) -> t) -> (t -> t1) -> t That doesn't make sense to me. Why are the type signatures

Two-layer “Y-style” combinator. Is this common? Does this have an official name?

情到浓时终转凉″ 提交于 2019-12-07 03:56:15
问题 I've been looking into how languages that forbid use-before-def and don't have mutable cells (no set! or setq ) can nonetheless provide recursion. I of course ran across the (famous? infamous?) Y combinator and friends, e.g.: http://www.ece.uc.edu/~franco/C511/html/Scheme/ycomb.html http://okmij.org/ftp/Computation/fixed-point-combinators.html http://www.angelfire.com/tx4/cus/combinator/birds.html http://en.wikipedia.org/wiki/Fixed-point_combinator When I went to implement "letrec" semantics

convert flip lambda into SKI terms

情到浓时终转凉″ 提交于 2019-12-05 19:37:08
I'm having trouble converting the lambda for flip into the SKI combinators (I hope that makes sense). Here is my conversion: /fxy.fyx /f./x./y.fyx /f./x.S (/y.fy) (/y.x) /f./x.S f (/y.x) /f./x.S f (K x) /f.S (/x.S f) (/x.K x) /f.S (/x.S f) K /f.S (S (/x.S) (/x.f)) K /f.S (S (K S) (K f)) K S (/f.S (S (K S) (K f))) (/f.K) S (/f.S (S (K S) (K f))) (K K) S (S (/f.S) (/f.S (K S) (K f))) (K K) S (S (K S) (/f.S (K S) (K f))) (K K) S (S (K S) (S (/f.S (K S)) (/f.K f))) (K K) S (S (K S) (S (/f.S (K S)) K)) (K K) S (S (K S) (S (S (/f.S) (/f.K S)) K)) (K K) S (S (K S) (S (S (K S) (/f.K S)) K)) (K K) S (S

Cleave a run-time computed value?

允我心安 提交于 2019-12-05 16:56:05
Cleave is a really useful combinator for minimising code duplication. Suppose I want to classify Abundant, Perfect, Deficient numbers : USING: arrays assocs combinators formatting io kernel math math.order math.primes.factors math.ranges sequences ; IN: adp CONSTANT: ADP { "deficient" "perfect" "abundant" } : proper-divisors ( n -- seq ) dup zero? [ drop { } ] [ divisors dup length 1 - head ] if ; : adp-classify ( n -- a/d/p ) dup proper-divisors sum <=> { +lt+ +eq+ +gt+ } ADP zip H{ } assoc-clone-like at ; : range>adp-classes ( n -- seq ) 1 swap 1 <range> [ adp-classify ] map ADP dup [ [ [ =

Python implementation of Parsec?

ぐ巨炮叔叔 提交于 2019-12-05 13:58:26
问题 I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the strings in the language. Given than I can control the state of the lexer from the grammar rules I think I'll be solving my use case using a look up table in the parser module, but it

fixed point combinator in lisp

无人久伴 提交于 2019-12-05 13:08:54
;; compute the max of a list of integers (define Y (lambda (w) ((lambda (f) (f f)) (lambda (f) (w (lambda (x) ((f f) x))))))) ((Y (lambda (max) (lambda (l) (cond ((null? l) -1) ((> (car l) (max (cdr l))) (car l)) (else (max (cdr l))))))) '(1 2 3 4 5)) I wish to understand this construction. Can somebody give a clear and simple explanation for this code? For example, supposing that I forget the formula of Y. How can I remember it , and reproduce it long after I work with it ? Will Ness Here's some related answers (by me): Y combinator discussion in "The Little Schemer" Unable to get