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
zero s z = z | |
succ n s z = s (n s z) | |
one s z = s z | |
two s z = s (s z) | |
false x y = y = zero x y | |
true x y = x = const x y | |
const x y = x |
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://stackoverflow.com/questions/10368835/partitioning-a-list-in-racket | |
foldr (\x r-> case r of [] -> [[x]]; ((a:_):_) | x+1 /= a -> [x]:r; (b:c) -> ((x:b):c)) [] | |
-- or, MUCH preferred, TRMC: | |
foldr (\x r-> let (b:c)=case r of {[] -> [[]]; ((a:_):_) | x+1 /= a -> []:r; _ -> r} in ((x:b):c)) [] | |
-- [1,2,3,5,6,8,9] ==> [[1,2,3],[5,6],[8,9]] |
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
{- Author: Jeff Newbern | |
Maintainer: Jeff Newbern <[email protected]> | |
Time-stamp: <Mon Nov 10 11:59:14 2003> | |
License: GPL | |
-} | |
{- DESCRIPTION | |
Example 1 - Our first monad |
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://stackoverflow.com/questions/15589556/why-are-difference-lists-not-an-instance-of-foldable | |
/15593349?noredirect=1#comment22277557_15593349 | |
a = singleton 1 singleton x = ChurchList $ \k z -> k x z | |
b = snoc a 2 snoc xs x = ChurchList $ \k z -> runList xs k (k x z) | |
c = snoc b 3 | |
d = append c c append u v = ChurchList $ \k z -> runList u k (runList v k z) | |
runList a k z = k 1 z a := ChurchList $ \k z -> k 1 z | |
runList b k z = runList a k (k 2 z) = k 1 (k 2 z) b := ChurchList $ \k z -> runList a k (k 2 z) |
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
hamm = 1:foldl (\s n->let r = merge s (n:map (n*) r) in r) [] [5,3,2] | |
r0 = [] | |
r1 = merge r0 (5: map (5*) r1) = [5^i | i<-[1..]] | |
r2 = merge r1 (3: map (3*) r2) = r1 + map(3*)(1:r1) + map(9*)(1:r1) + ... | |
+ map( 3^n *)(1:r1) + ... | |
r3 = merge r2 (2: map (2*) r3) = r2 + map(2*)(1:r2) + map(4*)(1:r2) + ... | |
+ map( 2^n *)(1:r2) + ... | |
so, given a factorization [(p_i,k_i)], the number's divisors are: |
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://stackoverflow.com/questions/15730385/scheme-pairs-output/15731206#15731206 | |
ipairs (x:xs) (y:ys) = (x,y) : g xs ys [map ((,) x) ys] where | |
g (x:xs) (y:ys) ac = (x,y) : map head ac ++ g xs ys (map ((,) x) ys : map tail ac) | |
ipairs1 (x:xs) (y:ys) = (x,y) : g xs ys [map ((,) x) ys] where | |
g (x:xs) (y:ys) ac = map head (reverse ac) ++ (x,y) : g xs ys (map ((,) x) ys : map tail ac) | |
In Scheme, |
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
{-# LANGUAGE PatternGuards #-} | |
-- triangular enumeration by upward diagonals | |
triags :: [[a] -> [a] | |
triags s = g s [] | |
where | |
g [] [] = [] | |
g s a = let (h,t) = splitAt 1 s | |
acc = filter (not.null) $ h ++ a | |
in map head acc ++ g t (map tail acc) |
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
"A cheeky person can thus express any infinitary recursion as a non-recursive foldr | |
on an infinite list. E.g., | |
foldr (\_ fibs -> 1 : 1 : zipWith (+) fibs (tail fibs)) undefined [1..] | |
(Edit: or, for that matter | |
foldr (\_ fib a b -> a : fib b (a + b)) undefined [1..] 1 1 | |
which is closer to the definition in the question.)" |
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
a note on dave4420's comment: | |
sequence [a,b] | |
= a >>= (\x -> b >>= (\y -> return [x,y])) | |
= a >>= (\x -> sequence [b] >>= (\xs -> return x:xs)) | |
For `Monad ((->) r)`, `(f >>= k) r = k (f r) r = (k.f) r r = join (k.f) r` | |
(makes sense, because in general, `f>>=k = join (fmap k f)`. | |
so, `sequence [a,b] r = (a >>= k1) r = k1 (a r) r`. |
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
I'm late to the party but here goes: | |
> divStep (start, n) = head $ [(x,q) | x <- [start .. isqrt n], | |
> let (q,r)=quotRem n x, r == 0] ++ [(n,1)] | |
> isqrt n = floor . sqrt . fromIntegral $ n | |
> pe3 n | n > 1 = fst . until ((== 1) . snd) divStep $ (2,n) | |
> factorizing n = takeWhile ((> 1) . fst) . drop 1 . iterate divStep $ (2,n) |