This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reverse xs = foldr (\x f rev -> f (x:rev)) id xs [] | |
-- with g = (\x f rev -> f (x:rev)) -- : | |
-- g a (g b (g c (g d id))) [] | |
-- g b (g c (g d id)) [a] | |
-- g c (g d id) [b,a] | |
-- g d id [c,b,a] | |
-- id [d,c,b,a] | |
-- [d,c,b,a] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://stackoverflow.com/questions/20568276/implement-insert-in-haskell-with-foldr | |
/20570385#20570385 | |
---- | |
You need a [paramorphism](http://stackoverflow.com/a/13317563/849891) for that: | |
para :: (a -> [a] -> b -> b) -> b -> [a] -> b | |
foldr :: (a -> b -> b) -> b -> [a] -> b | |
para c n (x : xs) = c x xs (para c n xs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# OPTIONS_GHC -O2 -fno-cse #-} | |
--------------------------------------------------------------------------------------------- | |
----- Sieve of Eratosthenes Comparison Table --- Treefold Merge with Wheel by Will Ness ----- | |
--- original linear-fold merging idea due to Richard Bird, in M. O'Neill JFP article | |
--- original tree-like folding idea due to Dave Bayer, on Haskell-cafe | |
--- double primes feed idea to prevent memoization/space leaks due to Melissa O'Neill | |
--- simplification of tree-folding formulation and wheel adaptation due to Will Ness | |
--- original Euler sieve one-liner due to Daniel Fischer on haskell-cafe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# OPTIONS_GHC -O2 -fno-cse #-} | |
--------------------------------------------------------------------------------------------- | |
----- Sieve of Eratosthenes Comparison Table --- Treefold Merge with Wheel by Will Ness ----- | |
--- original linear-fold merging idea due to Richard Bird, in M. O'Neill JFP article | |
--- original tree-like folding idea due to Dave Bayer, on Haskell-cafe | |
--- double primes feed idea to prevent memoization/space leaks due to Melissa O'Neill | |
--- simplification of tree-folding formulation and wheel adaptation due to Will Ness | |
--- original Euler sieve one-liner due to Daniel Fischer on haskell-cafe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
zebra(X,HS):- | |
length(HS,5), | |
member(H1,HS), nation(H1,eng), color(H1,red), | |
member(H2,HS), nation(H2,spa), owns( H2,dog), | |
member(H3,HS), drink( H3,coffee), color(H3,green), | |
member(H4,HS), nation(H4,ukr), drink(H4,tea), | |
member(H5,HS), smoke( H5,oldgold), owns( H5,snails), | |
member(H6,HS), smoke( H6,kools), color(H6,yellow), | |
member(H7,HS), smoke( H7,lucky), drink(H7,orange), | |
member(H8,HS), nation(H8,jpn), smoke(H8,parlamt), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (rev lst) | |
(if (or (null? lst) | |
(null? (cdr lst))) | |
lst | |
(apply (lambda (x . xs) | |
(apply (lambda (y . ys) | |
(cons y (rev (cons x (rev ys))))) | |
(rev xs))) | |
lst))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (rplac1 xs a b) | |
(let g ((xs xs) (f #f) (k (lambda (x y) x))) ; http://ideone.com/AbWKxS | |
(cond | |
(f (k xs f)) ; shortcut! | |
((null? xs) (k xs f)) ; http://stackoverflow.com/q/16550176/849891 | |
((not (pair? xs)) ; http://stackoverflow.com/q/16444290/849891 | |
(if (eq? xs a) (k b #t) (k xs f))) ; not f! | |
(else | |
(g (car xs) f (lambda (x f) | |
(g (cdr xs) f (lambda (y f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = filter (`notElem`s) x -- NOT | |
s = foldr (\(n,a) r-> if (a `notElem` take n s) then a:r else r) [] $ zip [0..] x -- not quite, yet | |
s = let a = [ [e | e `notElem` take n s] | (n,e) <- zip c x] -- here it is! | |
b = map length a -- \ | |
c = scanl (+) 0 b -- \ | |
s = concat a -- _this_ `s` | |
in s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
primes = ($[3,5..]) $ (id &&& map (\x->[x*x,x*x+2*x..]) | |
>>> second (foldi (\(x:xs)->(x:).union xs) []) | |
>>> (2:).uncurry minus | |
Prelude Saga Control.Arrow Control.Applicative> ($[3,5..]) $ (id&&&id) >>> second | |
(map (\x->[x*x, x*x+2*x..])) >>> second (foldi (\(x:xs)->(x:).union xs) []) >>> | |
(2:).uncurry minus >>> drop 160000 >>> take 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-26.html#%_thm_4.21 | |
(\n. ((\fact. fact fact n) (\ft k. if (= k 1) 1 (* k (ft ft (- k 1)))) )) | |
(\n. ( (\x g. g g x) n (\ft k. if (= k 1) 1 (* k (ft ft (- k 1)))) )) | |
(\g x. g g x) (\ft k. if (= k 1) 1 (* k (ft ft (- k 1)))) | |