Skip to content

Instantly share code, notes, and snippets.

fmap :: (a->b) -> foo a -> foo b (a->b) -> (p->a) -> (p->b)
fmap :: (c->d) -> bar c -> bar d (c->d) -> (q->c) -> (q->d)
(.) :: (t->u) -> (s->t) -> (s->u)
(.) fmap :: -- t ~ (a->b) ; u ~ (foo a->foo b)
(s->a->b) -> (s->foo a->foo b) (s->a->b) -> (s->(p->a)->(p->b))
(.) fmap fmap :: s ~ (c->d) ; a ~ (bar c) b ~ (bar d)
( (c->d) -> foo(bar c) -> foo(bar d) )
@WillNess
WillNess / gist:5019652
Created February 23, 2013 13:02
How do you define a function of signature h :: M Int -> M Int -> M Int so that h (M x) (M y) = M (x+y) without unwrapping the monad? http://stackoverflow.com/questions/15035468/how-do-you-define-a-function-of-signature-h-m-int-m-int-m-int-so-that-h
g x y = (return . (+x)) =<< y
= (=<<) (return . (+x)) y
= ((=<<) . (return .)) ((+) x) y
= (=<<) . (return .) . (+) $ x y
h mx my = mx >>= (\x ->
my >>= (\y ->
return (x+y) ))
h mx my = do x <- mx
@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 / 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`.