-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib
280 lines (247 loc) · 6.71 KB
/
stdlib
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
(defun range (a b)
;; Example:
;; (range 1 4) -> (list 1 2 3)
;; (range 4 1) -> (list 4 3 2)
(if (eq a b)
nil
(if (lt a b)
(cons a (range (+ a 1) b))
(cons a (range (+ a (- 1)) b)))))
(defun count (coll)
;; Example: (count (list 1 2))
(if (null? coll)
0
(+ 1 (count (tail coll)))))
(defun matches (f coll)
;; Example: (matches (lambda (x) (eq 2 x)) (list 2 1 2))
(if (null? coll)
0
(if (f (head coll))
(+ 1 (matches f (tail coll)))
(matches f (tail coll)))))
(defun at-index (i coll)
;; Example: (at-index 1 (list 'a' 'b' 'c'))
(if (or (null? coll) (lt i 0))
nil
(if (eq i 0)
(first coll)
(at-index (+ i (- 1)) (tail coll)))))
(defun map (f coll)
(if (null? coll)
nil
(cons
(f (first coll))
(map f (tail coll)))))
(defun filter (f coll)
(if (null? coll)
nil
(if (f (first coll))
(cons
(first coll)
(filter f (tail coll)))
(filter f (tail coll)))))
(defun for-each (f coll)
(if (null? coll)
nil
(begin
(f (head coll))
(for-each f (tail coll)))))
(defun fold-left (f seed coll)
;; Example: (fold-left + 0 (range 1 5))
(if (null? coll)
seed
(fold-left
f
(f seed (head coll))
(tail coll))))
(defun fold-right (f seed coll)
;; Example: (fold-right * 1 (range 1 5))
(if (null? coll)
seed
(f
(fold-right f seed (tail coll))
(head coll))))
(defun prepend (x coll)
(cons x coll))
(defun append (x coll)
;; Example: (append 5 (append 4 (append 3 nil)))
(if (null? coll)
(list x)
(cons
(head coll)
(append x (tail coll)))))
(defun zip (xs ys)
;; Example:
;; Balanced: (zip (list 1 2 3) (list 'a' 'b' 'c'))
;; Unbalanced: (zip (list 1 2 3) (list 'a' 'b'))
(if (or ;; short-circuit if unbalanced
(null? xs)
(null? ys))
nil
(cons
(list
(head xs)
(head ys))
(zip (tail xs) (tail ys)))))
(defun take (n coll)
;; Example: (take 2 (list 1 2 3))
(if (or
(null? coll)
(lte n 0))
nil
(cons
(head coll)
(take (+ n (- 1)) (tail coll)))))
(defun skip (n coll)
;; Example: (skip 2 (list 1 2 3))
(cond
((null? coll) nil)
((lte n 0) coll)
(else
(skip (+ n (- 1)) (tail coll)))))
(defun reverse (coll)
;; Example: (reverse (list 1 2 3))
(begin
(defun iter (acc xs)
(if (null? xs)
acc
(iter
(cons (head xs) acc)
(tail xs))))
(iter nil coll)))
(defun any (f coll)
;; Example: (any (lambda (x) (eq x 0)) (list 1 2 0))
(if (null? coll)
#f
(or
(f (head coll))
(any f (tail coll)))))
(defun all (f coll)
;; Example: (all (lambda (x) (gte x 0)) (list 1 2 0))
(if (null? coll)
#t
(and
(f (head coll))
(all f (tail coll)))))
(defun concat (xs ys)
;; Example: (concat (list 1 2) (list 3 4))
(if (null? xs)
ys
(cons
(head xs)
(concat (tail xs) ys))))
(defun group-n (n coll)
;; Example: (group-n 2 (range 1 7))
(begin
(defun group (n coll)
(cond
((null? coll) (list nil nil))
((lte n 0) (list nil coll))
(else
(begin
(define r
(group (+ n (- 1)) (tail coll)))
(list
(cons (head coll) (first r))
(second r))))))
(defun iter (n coll)
(begin
(define pair (group n coll))
(if (null? (second pair))
(list (first pair))
(cons
(first pair)
(iter n (second pair))))))
(if (lte n 0)
coll ;; just return original
(iter n coll))))
(defun flat-map (f coll)
;; Example: (flat-map (lambda (x) (if (eq x 2) (list x x) x)) (list 1 2 3))
(if (null? coll)
nil
(begin
(define r (f (head coll)))
(if (cell? r)
(concat r (flat-map f (tail coll)))
(cons
r
(flat-map f (tail coll)))))))
(defun flatten (coll)
;; Example: (flatten (list (list (list 1 2) 3) 4 (list 5 (list 6 (list 7)) 8)))
(if (null? coll)
nil
(if (cell? (head coll))
(concat
(flatten (head coll))
(flatten (tail coll)))
(cons
(head coll)
(flatten (tail coll))))))
(defun seed-n (seed n)
;; Example: (seed-n 'a' 10)
(if (lte n 0)
nil
(cons
seed
(seed-n seed (+ n (- 1))))))
(defun assoc (coll)
;; Example: (assoc (list 'foo' 'bar' 'baz'))
(begin
(defun iter (i xs)
(if (null? xs)
nil
(cons
(list i (head xs))
(iter (+ 1 i) (tail xs)))))
(iter 0 coll)))
(defun find-index (f coll)
;; Example: (find-index (lambda (x) (eq x 2)) (list 1 2 3 4))
(begin
(defun iter (i f xs)
(if (null? xs)
(- 1)
(if (f (head xs))
i
(iter (+ 1 i) f (tail xs)))))
(iter 0 f coll)))
(defun find (f coll)
;; Example: (find (lambda (x) (gt x 2)) (list 1 2 3 4))
(if (null? coll)
nil
(if (f (head coll))
(head coll)
(find f (tail coll)))))
(defun find-all (f coll)
;; Example: (find-all (lambda (x) (gt x 2)) (list 3 2 4 1))
(filter f coll))
(defun and-then (f g)
(lambda (x)
(f (g x))))
(defun compose (f g)
(lambda (x)
(g (f x))))
(defun identity (x) x)
(defun invoke (f)
(f))
(defun call (f args)
;; Example:
;; (defun add (a b) (+ a b))
;; (call add (list 1 2))
(if (null? args)
(f)
(read
(list
(concat (quote f) args)))))
(defun partial (f x)
(begin
(defun iter (n f args)
(if (gte (count args) n)
(call f args)
(lambda (x)
(begin
(define rest (flat-map identity (list x)))
(iter n f (concat args rest))))))
(define rest (flat-map identity (list x)))
(iter (arity f) f rest)))
(defun max-arity ()
2147483591)