-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib.lisp
338 lines (290 loc) · 7.98 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
;;
;; stdlib.lisp - Standard library executed alongside any user-supplied
;; program.
;;
;; This implements behaviour which is useful for users.
;;
;; There is a built in `type` function which returns the type of an object.
;;
;; Use this to define some simple methods to test argument-types
(define boolean? (lambda (x) (eq (type x) "boolean")))
(define error? (lambda (x) (eq (type x) "error")))
(define function? (lambda (x) (or
(list
(eq (type x) "procedure(lisp)")
(eq (type x) "procedure(golang)")))))
(define hash? (lambda (x) (eq (type x) "hash")))
(define macro? (lambda (x) (eq (type x) "macro")))
(define list? (lambda (x) (eq (type x) "list")))
(define number? (lambda (x) (eq (type x) "number")))
(define string? (lambda (x) (eq (type x) "string")))
(define symbol? (lambda (x) (eq (type x) "symbol")))
;; We've defined "<" in golang, we can now implement the missing
;; functions in terms of that:
;;
;; >
;; <=
;; >=
;;
(define > (lambda (a b) (< b a)))
(define >= (lambda (a b) (! (< a b))))
(define <= (lambda (a b) (! (> a b))))
;;
;; This is a bit sneaky. NOTE there is no short-circuiting here.
;;
;; Given a list use `filter` to return those items which are "true".
;;
;: If the length of the input list, and the length of the filtered list
;; are the same then EVERY element was true so our AND result is true.
;;
(define and (lambda (xs:list)
(let ((res nil))
(set! res (filter xs (lambda (x) (if x true false))))
(if (= (length res) (length xs))
true
false))))
;;
;; This is also a bit sneaky. NOTE there is no short-circuiting here.
;;
;; Given a list use `filter` to return those items which are "true".
;;
;; If the output list has at least one element that was true then the
;; OR result is true.
;;
(define or (lambda (xs:list)
(let ((res nil))
(set! res (filter xs (lambda (x) (if x true false))))
(if (> (length res) 0)
true
false))))
;; Traditionally we use `car` and `cdr` for accessing the first and rest
;; elements of a list. For readability it might be nice to vary that
(define first (lambda (x:list) (car x)))
(define rest (lambda (x:list) (cdr x)))
;; inc/dec are useful primitives to have
(define inc (lambda (n:number) (+ n 1)))
(define dec (lambda (n:number) (- n 1)))
;; Not is useful
(define ! (lambda (x) (if x #f #t)))
;; Some simple tests of numbers
(define zero? (lambda (n) (= n 0)))
(define one? (lambda (n) (= n 1)))
(define even? (lambda (n) (zero? (% n 2))))
(define odd? (lambda (n) (! (even? n))))
;; Square root
(define sqrt (lambda (x:number) (# x 0.5)))
;;
;; if2 is a simple macro which allows you to run two actions if an
;; (if ..) test succeeds.
;;
;; This means you can write:
;;
;; (if2 true (print "1") (print "2"))
;;
;; Instead of having to use (begin), like so:
;;
;; (if true (begin (print "1") (print "2")))
;;
;; The downside here is that you don't get a negative branch, but running
;; two things is very common - see for example the "(while)" and "(repeat)"
;; macros later in this file.
;;
(define if2 (macro (pred one two)
`(if ~pred (begin ~one ~two))))
;;
;; Part of our while-implementation.
;; If the specified predicate is true, then run the body.
;;
;; NOTE: This recurses, so it will eventually explode the stack.
;;
;; NOTE: We use "if2" not "if".
;;
(define while-fun (lambda (predicate body)
(if2 (predicate)
(body)
(while-fun predicate body))))
;;
;; Now a macro to use the while-fun body as part of a while-function
;;
;; NOTE: We use "if2" not "if".
;;
(define while (macro (expression body)
(list 'while-fun
(list 'lambda '() expression)
(list 'lambda '() body))))
;; Setup a simple function to run a loop N times
;;
;; NOTE: We use "if2" not "if".
;;
(define repeat (lambda (n body)
(if2 (> n 0)
(body n)
(repeat (- n 1) body))))
;; A useful helper to apply a given function to each element of a list.
(define apply (lambda (lst:list fun:function)
(if (nil? lst)
()
(begin
(fun (car lst))
(apply (cdr lst) fun)))))
;; A helper to apply a function to each key/value pair of a hash
(define apply-hash (lambda (hs:hash fun:function)
(let ((lst (keys hs)))
(apply lst (lambda (x) (fun x (get hs x)))))))
;; Return the length of the given string or list.
(define length (lambda (arg)
(if (list? arg)
(begin
(if (nil? arg) 0
(inc (length (cdr arg)))))
0
)))
;; Find the Nth item of a list
(define nth (lambda (lst:list i:number)
(if (= 0 i)
(car lst)
(nth (cdr lst) (- i 1)))))
;; More mathematical functions relating to negative numbers.
(define neg (lambda (n:number) (- 0 n)))
(define neg? (lambda (n:number) (< n 0)))
(define pos? (lambda (n:number) (> n 0)))
(define abs (lambda (n:number) (if (neg? n) (neg n) n)))
(define sign (lambda (n:number) (if (neg? n) (neg 1) 1)))
;; Create ranges of numbers in a list
(define range (lambda (start:number end:number step:number)
(if (< start end)
(cons start (range (+ start step) end step))
())))
;; Create sequences from 0/1 to N
(define seq (lambda (n:number) (range 0 n 1)))
(define nat (lambda (n:number) (range 1 n 1)))
;; Remove items from a list where the predicate function is not T
(define filter (lambda (xs:list f:function)
(if (nil? xs)
()
(if (f (car xs))
(cons (car xs)(filter (cdr xs) f))
(filter (cdr xs) f)))))
;; Replace a list with the contents of evaluating the given function on
;; every item of the list
(define map (lambda (xs:list f:function)
(if (nil? xs)
()
(cons (f (car xs)) (map (cdr xs) f)))))
;; reduce function
(define reduce (lambda (xs f acc)
(if (nil? xs)
acc
(reduce (cdr xs) f (f acc (car xs))))))
;; Now define min/max using reduce
(define min (lambda (xs:list)
(if (nil? xs)
()
(reduce xs
(lambda (a b)
(if (< a b) a b))
(car xs)))))
(define max (lambda (xs:list)
(if (nil? xs)
()
(reduce xs
(lambda (a b)
(if (< a b) b a))
(car xs)))))
; O(n^2) behavior with linked lists
(define append (lambda (xs:list el)
(if (nil? xs)
(list el)
(cons (car xs) (append (cdr xs) el)))))
(define reverse (lambda (x:list)
(if (nil? x)
()
(append (reverse (cdr x)) (car x)))))
;;
;; This is either gross or cool.
;;
;; Define a hash which has literal characters and their upper-case, and
;; lower-cased versions
;;
(define upper-table {
a "A"
b "B"
c "C"
d "D"
e "E"
f "F"
g "G"
h "H"
i "I"
j "J"
k "K"
l "L"
m "M"
n "N"
o "O"
p "P"
q "Q"
r "R"
s "S"
t "T"
u "U"
v "V"
w "W"
x "X"
y "Y"
z "Z"
} )
(define lower-table {
A "a"
B "b"
C "c"
D "d"
E "e"
F "f"
G "g"
H "h"
I "i"
J "j"
K "k"
L "l"
M "m"
N "n"
O "o"
P "p"
Q "q"
R "r"
S "s"
T "t"
U "u"
V "v"
W "w"
X "x"
Y "y"
Z "z"
} )
;; Translate the elements of the string using the specified hash
(define translate (lambda (x:string hsh:hash)
(let ((chrs (split x "")))
(join (map chrs (lambda (x)
(if (get hsh x)
(get hsh x)
x)))))))
;; Convert the given string to upper-case, via the lookup table.
(define upper (lambda (x:string)
(translate x upper-table)))
;; Convert the given string to upper-case, via the lookup table.
(define lower (lambda (x:string)
(translate x lower-table)))
;; This is required for our quote/quasiquote/unquote/splice-unquote handling
;;
;; Testing is hard, but
;;
;; (define lst (quote (b c))) ; b c
;; (print (quasiquote (a lst d))) ; (a lst d)
;; (print (quasiquote (a (unquote lst) d))) ; (a (b c) d)
;; (print (quasiquote (a (splice-unquote lst) d))) ; (a b c d)
;;
(define concat (lambda (seq1 seq2)
(if (nil? seq1)
seq2
(cons (car seq1) (concat (cdr seq1) seq2)))))