Skip to content

Instantly share code, notes, and snippets.

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]
@WillNess
WillNess / foldr-insert.hs
Last active October 22, 2017 04:41
foldr - insert - paramorphism
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)
{-# 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
{-# 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
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),
@WillNess
WillNess / rev.scm
Created November 13, 2013 20:56 — forked from roerd/rev.scm
(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)))
@WillNess
WillNess / rplac1.scm
Last active December 23, 2015 08:19
rplac1 - replace 1st occurrence in nested list
(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)
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
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
@WillNess
WillNess / Yderiv.hs
Last active December 21, 2015 22:28
yet another incomplete Y derivation
-- 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))))