-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib.unl
267 lines (233 loc) · 5.89 KB
/
stdlib.unl
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
(set-fn
list
(lambda list (& args)
args))
(set-fn
list*
(lambda list* (& args)
(apply (symbol-function (quote apply))
(symbol-function (quote list)) args)))
(set-fn
funcall
(lambda funcall (f & args)
(apply f args)))
(set-fn
append
(lambda append (x y)
(if (emptyp x)
y
(cons (first x)
(append (rest x) y)))))
(set-fn
reduce
(lambda reduce (f init list)
(if (emptyp list)
init
(reduce
f
(funcall f init (first list))
(rest list)))))
(set-fn
reverse-inner
(lambda (x acc)
(if (emptyp x)
acc
(reverse-inner
(rest x)
(cons (first x) acc)))))
(set-fn
reverse
(lambda reverse (x)
(reverse-inner x ())))
(set-fn
not
(lambda not (x)
(if x nil t)))
(set-macro-fn
unq
(lambda unq (x)
(error "unq outside of qquote")))
(set-macro-fn
unqs
(lambda unqs (x)
(error "unqs outside of qquote")))
(set-macro-fn
qquote
(lambda quote (x)
(qquote-process x)))
(set-fn
qquote-process
(lambda qquote-process (x)
(if (not (listp x))
(list (quote quote) x)
(if (emptyp x)
(list (quote quote) x)
(if (equal (first x) (quote qquote))
(qquote-process (qquote-process (first (rest x))))
(if (equal (first x) (quote unq))
(first (rest x))
(if (equal (first x) (quote unqs))
(error "unqs after qquote")
(qquote-transform-list x))))))))
(set-fn
qquote-transform-list
(lambda qquote-transform-list (x)
(qquote-transform-list-inner x ())))
(set-fn
qquote-transform-list-inner
(lambda qquote-transform-list-inner (x transformed-acc)
(if (emptyp x)
(list* (quote reduce) (quote (lambda (x y) (append x y)))
()
(list (cons (quote list) (reverse transformed-acc))))
(qquote-transform-list-inner
(rest x)
(cons (qquote-transform-list-item (first x))
transformed-acc)))))
(set-fn
qquote-transform-list-item
(lambda qquote-transform-list-item (x)
(if (not (listp x))
(list (quote list) (list (quote quote) x))
(if (emptyp x)
(list (quote list) (list (quote quote) x))
(if (equal (first x) (quote unq))
(list (quote list) (first (rest x)))
(if (equal (first x) (quote unqs))
(first (rest x))
(list (quote list) (qquote-process x))))))))
(set-macro-fn
defmacro
(lambda defmacro (name args & body)
(qquote
(set-macro-fn
(unq name)
(lambda (unq name) (unq args)
(unqs body))))))
(defmacro defun (name args & body)
(qquote
(set-fn
(unq name)
(lambda (unq name) (unq args)
(unqs body)))))
(defun fibo (n)
(if (equal n 1)
1
(if (equal n 0)
1
(+ (fibo (- n 1))
(fibo (- n 2))))))
(defmacro strange-let (bindings & body)
(reduce
(lambda (acc binding)
(let ((sym (first binding))
(val (first (rest binding))))
(qquote
(funcall
(lambda ((unq sym))
(unq acc))
(unq val)))))
(qquote (let () (unqs body)))
(reverse bindings)))
(defmacro symf (sym)
(qquote
(symbol-function (unq sym))))
(defmacro cond (& clauses)
(reduce
(lambda (acc clause)
(qquote
(if (unq (first clause))
(let ()
(unqs (rest clause)))
(unq acc))))
(quote nil)
(reverse clauses)))
(defmacro and (& forms)
(let ((reversed (if (emptyp forms)
(list t)
(reverse forms))))
(reduce
(lambda (acc form)
(qquote
(if (unq form) (unq acc) nil)))
(first reversed)
(rest reversed))))
(defmacro or (& forms)
(let ((reversed (if (emptyp forms)
(list nil)
(reverse forms))))
(reduce
(lambda (acc form)
(qquote
(let ((eform (unq form)))
(if eform eform (unq acc)))))
(first reversed)
(rest reversed))))
(defmacro when (c & body)
(qquote
(if (unq c)
(let ()
(unqs body)))))
(defun second (list)
(first (rest list)))
(defmacro if-let (binding then & else)
(qquote
(let ((unq binding))
(if (unq (first binding))
(unq then)
(unqs else)))))
(defmacro when-let (binding & body)
(qquote
(if-let (unq binding)
(let () (unqs body))
nil)))
(defmacro if-it (cond then & else)
(qquote
(if-let (it (unq cond))
(unq then)
(unqs else))))
(defun every? (pred coll)
(or (emptyp coll)
(and (funcall pred (first coll))
(every? pred (rest coll)))))
(defun mapcar-single (f xs)
(if (emptyp xs)
xs
(cons (funcall f (first xs))
(mapcar-single f (rest xs)))))
(defun mapcar (f xs & ys)
(let ((lists (cons xs ys)))
(if (every? (lambda (x) (not (emptyp x)))
lists)
(let ((heads (mapcar-single (symf (quote first))
lists))
(tails (mapcar-single (symf (quote rest))
lists)))
(cons (apply f heads)
(apply (symf (quote mapcar)) f tails)))
())))
(defmacro dbgp (str-prefix & forms)
(qquote
(let ()
(unqs
(mapcar
(lambda (form)
(qquote
(let ()
(when (unq str-prefix)
(stdout-write (unq str-prefix))
(stdout-write " "))
(print (quote (unq form)))
(stdout-write " = ")
(println (unq form)))))
forms)))))
(defmacro dbg (& forms)
(qquote (dbgp nil (unqs forms))))
(defun range (to)
(let ((loop (lambda (to acc)
(if (equal to 0)
(cons 0 acc)
(funcall loop (- to 1)
(cons to acc))))))
(funcall loop to ())))
(defmacro comment (& body))