This file contains hidden or 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
| divs n = concat $ unfoldr (\(n,i)->case n of 1->Nothing; | |
| _->if i*i>n then Just([n], (1,i)) else case rem n i of {0->Just([i], (div n i,i) | |
| ); _->Just([], (n,i+1))}) (n,2) |
This file contains hidden or 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
| Prelude Control.Arrow Control.Monad Control.Applicative> (tail >>= zip) [1..5] | |
| [(2,1),(3,2),(4,3),(5,4)] | |
| Prelude Control.Arrow Control.Monad Control.Applicative> liftM2 zip tail id [1..5] | |
| [(2,1),(3,2),(4,3),(5,4)] | |
| Prelude Control.Arrow Control.Monad Control.Applicative> liftA2 zip tail id [1..5] | |
| [(2,1),(3,2),(4,3),(5,4)] | |
| Prelude Control.Arrow Control.Monad Control.Applicative> (zip.tail) <*> id $ [1..5] | |
| [(2,1),(3,2),(4,3),(5,4)] | |
| Prelude Control.Arrow Control.Monad Control.Applicative> zip <$> tail <*> id $ [1..5] | |
| [(2,1),(3,2),(4,3),(5,4)] |
This file contains hidden or 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)))) | |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |