-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.lisp
105 lines (79 loc) · 1.56 KB
/
stdlib.lisp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(def {nil} {})
(def {true} 1)
(def {false} 0)
(def {fun} (\ {decl body} {
def (head decl) (\ (tail decl) body)
}))
(fun {unpack f l} {
eval (join (list f) l)
})
(fun {pack f & xs} {f xs})
(def {curry} unpack)
(def {uncurry} pack)
(fun {do & l} {
if (== l nil)
{nil}
{last l}
})
(fun {let b} {
((\ {_} b) ())
})
(fun {not x} {- 1 x})
(fun {and x y} {* x y})
(fun {or x y} {+ x y})
(fun {flip f a b} {f b a})
(fun {ghost & xs} {eval xs})
(fun {comp f g x} {f (g x)})
(fun {fst l} {eval (head l)})
(fun {snd l} {eval (head (tail l))})
(fun {trd l} {eval (head (tail (tail l)))})
(fun {len l} {
if (== l nil)
{0}
{+ 1 (len (tail l))}
})
(fun {nth n l} {
if (== n 0)
{fst l}
{nth (- n 1) (tail l)}
})
(fun {last l} {nth (- (len l) 1) l})
(fun {take n l} {
if (== n 0)
{nil}
{join (head l) (take (- n 1) l)}
})
(fun {drop n l} {
if (== n 0)
{l}
{drop (- n 1) l}
})
(fun {split n l} {list (take n l) (drop n l)})
(fun {elem x l} {
if (== l nil)
{false}
{if (== x (fst l)) {true} {elem x (tail l)}}
})
(fun {map f l} {
if (== l nil)
{nil}
{join (list (f (fst l))) (map f (tail l))}
})
(fun {filter f l} {
if (== l nil)
{nil}
{join (if (f (fst l)) {head l} {nil}) (filter f (tail l))}
})
(fun {foldl f z l} {
if (== l nil)
{z}
{foldl f (f z (fst l)) (tail l)}
})
(fun {sum l} {foldl + 0 l})
(fun {product l} {foldl * 1 l})
(fun {select & cs} {
if (== cs nil)
{error "No selection Found"}
{if (fst (fst cs)) {snd (fst cs)} {unpack select (tail cs)}}
})
(def {otherwise} true)