-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.lambda
36 lines (34 loc) · 1.31 KB
/
list.lambda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
foldr = λf.λi.λl.Y (λr.λl.l (λh.λt.λi.f h (r t)) i) l
map = λf.λl.foldr (λe.λa.cons (f e) a) nil l
concat = λa.λb.foldr cons b a
rcons = λa.λb.concat a (cons b nil)
concatmap = λf.λl.foldr concat nil (map f l)
length = λl.foldr (λe.λa.succ(a)) 0 l
filter = λf.λl.foldr (λe.λa.(f e) (cons e a) a) nil l
reverse = λl.foldr (C rcons) nil l
some = λf.λl.foldr or false (map f l)
all = λf.λl.foldr and true (map f l)
none = λf.λl.(not (some f l))
replicate = λn.λe.Y (λr.λn.(iszero(n) nil (cons e (r (pred n))))) n
find = λp.λl.foldr (λe.λa.(p e) e a) nil l
unfoldr = λf.λi.λt.Y (λr.λx.(t x) nil (cons x (r (f x)))) i
iota = λn.unfoldr succ 0 (eq n)
mapiota = λf.λn.map f (iota n)
last = λl.Y (λr.λl.if (null (cdr l)) (car l) (r (cdr l))) l
butlast = λl.Y (λr.λl.if (null (cdr l)) nil (cons (car l) (r (cdr l)))) l
rotatel = λl.rcons (cdr l) (car l)
rotater = λl.concat (last l) (butlast l)
dip = λx.λl.(nth 0 l) x
rep = λl.(nth 0 l) (nth 0 l)
run = λl.(nth 0 l) l
sap = λa.λb.(nth 0 b) (nth 0 a)
first = compose car (0 cdr)
second = compose car (1 cdr)
third = compose car (2 cdr)
fourth = compose car (3 cdr)
fifth = compose car (4 cdr)
sixth = compose car (5 cdr)
seventh = compose car (6 cdr)
eighth = compose car (7 cdr)
ninth = compose car (8 cdr)
tenth = compose car (9 cdr)