-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.lspy
211 lines (173 loc) · 3.73 KB
/
stdlib.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
; vim: ft=lisp
;;;
;;; Lispy Standard Prelude
;;;
;;; Functional Functions
; Open new scope
; This is useful with = for closure
; let {do (= {x} 100) (x)}
(fun {let b} {
((\ {_} b) ())
})
; Unpack List to Function
(fun {unpack func l} {
eval (join (list func) l)
})
; Unapply List to Function
(fun {pack func & xs} {func xs})
; Curried and Uncurried calling
(def {curry} unpack)
(def {uncurry} pack)
; Perform Several things in Sequence
(fun {do & l} {
cond (== l nil)
{nil}
{last l}
})
;;; Numeric Functions
; Minimum of Arguments
(fun {min & xs} {
cond (== (tail xs) nil) {fst xs}
{do
(= {rest} (unpack min (tail xs)))
(= {item} (fst xs))
(cond (< item rest) {item} {rest})
}
})
; Maximum of Arguments
(fun {max & xs} {
cond (== (tail xs) nil) {fst xs}
{do
(= {rest} (unpack max (tail xs)))
(= {item} (fst xs))
(cond (> item rest) {item} {rest})
}
})
;;; Conditional Functions
(fun {select & cs} {
cond (== cs nil)
{error "No Selection Found"}
{cond (fst (fst cs)) {snd (fst cs)} {unpack select (tail cs)}}
})
(fun {case x & cs} {
cond (== cs nil)
{error "No Case Found"}
{cond (== x (fst (fst cs))) {snd (fst cs)} {
unpack case (join (list x) (tail cs))}}
})
(def {otherwise} t)
;;; Misc Functions
(fun {flip func a b} {func b a})
(fun {ghost & xs} {eval xs})
(fun {comp func g x} {func (g x)})
;;; List Functions
; First, Second, or Third Item in List
(fun {fst l} { eval (head l) })
(fun {snd l} { eval (head (tail l)) })
(fun {trd l} { eval (head (tail (tail l))) })
; Nth item in List
(fun {nth n l} {
cond (== n 0)
{fst l}
{nth (- n 1) (tail l)}
})
; Last item in List
(fun {last l} {nth (- (len l) 1) l})
; Apply Function to List
(fun {map func l} {
cond (== l nil)
{nil}
{join (list (func (fst l))) (map func (tail l))}
})
; Apply Filter to List
(fun {filter func l} {
cond (== l nil)
{nil}
{join (cond (func (fst l)) {head l} {nil}) (filter func (tail l))}
})
; Reverse List
(fun {reverse l} {
cond (== l nil)
{nil}
{join (reverse (tail l)) (head l)}
})
; Fold Left
(fun {foldl func z l} {
cond (== l nil)
{z}
{foldl func (func z (fst l)) (tail l)}
})
; Fold Right
(fun {foldr func z l} {
cond (== l nil)
{z}
{func (fst l) (foldr func z (tail l))}
})
(fun {sum l} {foldl + 0 l})
(fun {product l} {foldl * 1 l})
; Take N items
(fun {take n l} {
cond (== n 0)
{nil}
{join (head l) (take (- n 1) (tail l))}
})
; Drop N items
(fun {drop n l} {
cond (== n 0)
{l}
{drop (- n 1) (tail l)}
})
; Split at N
(fun {split n l} {list (take n l) (drop n l)})
; Take While
(fun {take-while func l} {
cond (! (unpack func (head l)))
{nil}
{join (head l) (take-while func (tail l))}
})
; Drop While
(fun {drop-while func l} {
cond (! (unpack func (head l)))
{l}
{drop-while func (tail l)}
})
; Element of List
(fun {elem x l} {
cond (== l nil)
{f}
{cond (== x (fst l)) {t} {elem x (tail l)}}
})
; Find element in list of pairs
(fun {lookup x l} {
cond (== l nil)
{error "No Element Found"}
{do
(= {key} (fst (fst l)))
(= {val} (snd (fst l)))
(cond (== key x) {val} {lookup x (tail l)})
}
})
; Zip two lists together into a list of pairs
(fun {zip x y} {
cond (|| (== x nil) (== y nil))
{nil}
{join (list (join (head x) (head y))) (zip (tail x) (tail y))}
})
; Unzip a list of pairs into two lists
(fun {unzip l} {
cond (== l nil)
{{nil nil}}
{do
(= {x} (fst l))
(= {xs} (unzip (tail l)))
(list (join (head x) (fst xs)) (join (tail x) (snd xs)))
}
})
;;; Other Fun
; Fibonacci
(fun {fib n} {
select
{ (== n 0) 0 }
{ (== n 1) 1 }
{ otherwise (+ (fib (- n 1)) (fib (- n 2))) }
})