Skip to content

Instantly share code, notes, and snippets.

@WillNess
WillNess / gist:5021669
Last active December 14, 2015 03:29
Church numerals mult m n s z = m (n s) z
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
@WillNess
WillNess / gist:5177263
Last active December 15, 2015 00:59
split l-o-n into chunks of consecutive numbers areas
-- 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]]
{- Author: Jeff Newbern
Maintainer: Jeff Newbern <jnewbern@nomaware.com>
Time-stamp: <Mon Nov 10 11:59:14 2003>
License: GPL
-}
{- DESCRIPTION
Example 1 - Our first monad
@WillNess
WillNess / ChurchList.hs
Last active June 7, 2017 20:44
Church-encoded Lists are efficient?
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)
@WillNess
WillNess / gist:5276388
Last active December 15, 2015 14:39
divisors, in order, with MERGE
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:
@WillNess
WillNess / gist:5281352
Last active December 15, 2015 15:19
interleaved-pairs in Scheme and Haskell
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,
@WillNess
WillNess / gist:5306459
Last active December 15, 2015 18:49
triags ... ... mapped patterns
{-# 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)
@WillNess
WillNess / gist:5347342
Last active December 16, 2015 00:28
Can fold be used to create infinite lists? -- by pigworker Sep 6 '12 at 11:37 -- http://stackoverflow.com/a/12299223/849891
"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.)"
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`.
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)