-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.lambda
51 lines (48 loc) · 2.05 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
foldr = λf.λi.λl.l (Y (λr.λh.λt.λz.f h (t r z))) i
map = λf.λl.l (Y (λr.λh.λt.λi.(λz.z (f h) (t r i)))) nil
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.if (f e) (cons e a) a) nil l
remove = λf.λl.filter (complement f) l
reverse = λl.l (Y (λr.λh.λt.λi.t r (λz.z h i))) nil
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))
notevery = λf.λl.(not (all f l))
find = λp.λl.foldr (λe.λa.if (p e) e a) nil l
unfoldr = λf.λi.λt.Y (λr.λx.if (t x) nil (cons x (r (f x)))) i
iota = λn.unfoldr succ 0 (eq n)
mapiota = λf.λn.map f (iota n)
replicate = λn.λe.mapiota (λx.e) n
range = λs.λe.mapiota (add s) (sub e s)
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.cons (last l) (butlast l)
take = λn.λl.Y (λr.λl.if (eq n (length l)) l (r (butlast l))) l
takewhile = λp.λl.Y (λr.λl.if (p (car l)) (cons (car l) (r (cdr l))) nil) l
dropwhile = λp.λl.Y (λr.λl.if (p (car l)) (r (cdr l)) l) l
split = λn.λl.(cons (take n l) (nthcdr n l))
unzip = λl.(cons (map car l) (map cdr l))
zip = λa.λb.a (Y (λr.λh.λt.λb.(cons (cons h (car b)) (t r (cdr b))))) b
ipairs = λl.zip (iota (length l)) l
juxt = λf.λg.λx.(cons (f x) (g x))
equal = λp.λa.λb.all (λe.e (λh.λt.λi.p h t) false) (zip a b)
%sortmerge = λl.λp.λx.l (Y (λr.λh.λt.λx.if (p h (car x)) (cons h (t r x)) (cons (car x) (cons h t)))) (cons x nil)
sort = λp.λl.foldr (λe.λa.%sortmerge a p e) nil 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 = B car (0 cdr)
second = B car (1 cdr)
third = B car (2 cdr)
fourth = B car (3 cdr)
fifth = B car (4 cdr)
sixth = B car (5 cdr)
seventh = B car (6 cdr)
eighth = B car (7 cdr)
ninth = B car (8 cdr)
tenth = B car (9 cdr)