-
Notifications
You must be signed in to change notification settings - Fork 0
/
prelude.lspy
193 lines (161 loc) · 3.29 KB
/
prelude.lspy
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
; Atoms
(def nil {})
; Function definition
(def fun (\! {name args body} {
def (name) (\ (eval args) (eval body))
}))
; Macro definition
(def fun! (\! {name args body} {
def (name) (\! (eval args) (eval body))
}))
; Unpack list for function
(fun unpack {f xs} {
eval (cons f xs)
})
; Pack list for function
(fun pack {f & xs} {f xs})
; Curried and uncurried calling
(def uncurry pack)
(def curry unpack)
; Perform several things in sequence
(fun do {& l} {
if (== l nil)
{nil}
{last l}
})
; Open a new scope
(fun let {body} {
((\ {_} body) ())
})
; Selects the first item that is true
(fun select {& cs} {
if (== cs nil)
{error "No selection found"}
{ if (fst (fst cs))
{snd (fst cs)}
{unpack select (tail cs)}
}
})
; Default case
(def otherwise true)
; Matches the argument against all the values
(fun case {x & xs} {
if (== xs nil)
{error "No case found"}
{ if (== x (fst (fst xs)))
{snd (fst xs)}
{unpack case (cons x (tail xs))}
}
})
; Logical functions
(fun and {x y} { if x {y} {false} })
(fun or {x y} { if x {true} {y} })
(fun not {x} { == x false })
; Miscellaneous functions
; Flip the first two arguments
(fun flip {f x y & xs} {
unpack f (join {y x} xs)
})
; Just evaluates its arguments
(fun ghost {& xs} {eval xs})
; Function composition
(fun comp {f g x & xs} {
f (unpack g (cons x xs))
})
; List finctions
; First, second, or third item in iist
(fun fst {l} {eval (head l)})
(fun snd {l} {eval (head (tail l))})
(fun trd {l} {eval (head (tail (tail l)))})
; Nth item of a list
(fun nth {n l} {
if (== n 0)
{fst l}
{nth (- n 1) (tail l)}
})
; Last item of a list
(fun last {l} {nth (- (len l) 1) l})
; Take N items
(fun take {n l} {
if (== l nil)
{nil}
{ if (== n 0)
{nil}
{join (head l) (take (- n 1) (tail l))}
}
})
; Drop N items
(fun drop {n l} {
if (== l nil)
{nil}
{ if (== n 0)
{l}
{drop (- n 1) (tail l)}
}
})
; Split at N
(fun split {n l} {list (take n l) (drop n l)})
; Check if element is in list
(fun elem {x l} {
if (== l nil)
{false}
{ if (== x (fst l))
{true}
{elem x (tail l)}
}
})
; Apply function to list
(fun map {f l} {
if (== l nil)
{nil}
{cons (f (fst l)) (map f (tail l))}
})
; Apply filter to list
(fun filter {p l} {
if (== l nil)
{nil}
{ if (p (fst l))
{cons (fst l) (filter p (tail l))}
{filter p (tail l)}
}
})
; Fold to the left
(fun foldl {f acc l} {
if (== l nil)
{acc}
{foldl f (f acc (fst l)) (tail l)}
})
; Fold to the right
(fun foldr {f acc l} {
if (== l nil)
{acc}
{foldr f (f (fst l) acc) (tail l)}
})
; Reverse of a list
(fun reverse {l} {
if (== l nil)
{nil}
{join (reverse (tail l)) (head l)}
})
; Calculates the sum of a list
(def sum (unpack +))
; Calculates the product of a list
(def product (unpack *))
; Fibonacci
(fun fib {n} {
select
{ (== n 0) 0 }
{ (== n 1) 1 }
{ otherwise (+ (fib (- n 1)) (fib (- n 2))) }
})
; Wheather a number is even
(fun even {n} {== (% n 2) 0})
; Wheather a number is odd
(fun odd {n} {not (even n)})
; Comparison function wrappers to allow partial application
(fun eq {x y} {== x y})
(fun ne {x y} {!= x y})
(fun gt {x y} {> x y})
(fun lt {x y} {< x y})
(fun ge {x y} {>= x y})
(fun le {x y} {<= x y})