-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.lisp
309 lines (239 loc) · 8.99 KB
/
base.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
(in-package :arc-compat.internal)
(in-readtable :standard)
(in-suite arc-compat)
(defmacro mac (name args &body body)
"Creates a macro."
`(defmacro ,name ,(cl:if (listp args)
(arc-ll-to-cl-ll args)
`(&rest ,args))
#|(arnesi:with-lisp1 ,@body)|#
,@body))
(defmacro def (name args &body body)
`(defun ,name (,@(cl:if (listp args)
(arc-ll-to-cl-ll args)
`(&rest ,args)))
,@body))
(defmacro if (&rest args)
(cond ((null args) ''nil)
((null (cdr args)) (car args))
(T `(cl:if ,(car args)
,(cadr args)
(if ,@(cddr args))))))
(defmacro or (&rest args)
`(cl:or ,@args))
(defmacro and (&rest args)
`(cl:and ,@args))
(defalias atom cl:atom)
(defalias complement cl:complement)
(defmacro when (pred &body body)
`(if ,pred (cl:progn ,@body)))
(defmacro aif (&rest args)
(cond ((null args) ''nil)
((null (cdr args)) (car args))
(T `(cl:let ((it ,(car args)))
(cl:if (not (null it))
,(cadr args)
(aif ,@(cddr args)))))))
(tst aif
(== (macroexpand '(aif t)) T)
(== (aif t) t))
;(defmacro +INTERNAL-DEPARAM (param &body body)
; (DESTRUCTURING-BIND ,param ,g
; (DECLARE (IGNORABLE ,@(+internal-flatten args)))
; ,@body))
(defmacro FN (args &body body)
(cl:let ((g (gensym "fn-")))
(cond ((null args)
`(LAMBDA () ,@body))
((atom args)
`(LAMBDA (&rest ,args) ,@body))
((cl:and (cl:tailp () args)
(cl:every #'cl:atom args))
`(LAMBDA (,@args)
(cl:declare (cl:ignorable ,@args))
,@body))
(T `(LAMBDA (&rest ,g)
(CL:DECLARE (CL:DYNAMIC-EXTENT ,g))
(DESTRUCTURING-BIND ,args ,g
(cl:DECLARE
(cl:IGNORABLE ,@(remove-if (lambda (x)
(member x cl:lambda-list-keywords))
(+internal-flatten args))))
,@body))))))
(5am:test fn
(== (macroexpand '(fn () foo))
'#'(lambda () foo)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun _-to-gensym (list)
(cond ((atom list) list)
((consp (car list))
(cons (\_-to-gensym (car list))
(\_-to-gensym (cdr list))))
(T (cons (if (string= "_" (car list))
(cl:gensym "_")
(car list))
(\_-to-gensym (cdr list)))))))
(5am:test _-to-gensym
(== (_-to-gensym '(x y z))
'(X Y Z))
(== (_-to-gensym 'x)
'X)
(== (subst-if-not '_ (lambda (x)
(cl:if (consp x)
x
(symbol-package x)))
(_-to-gensym '(_ a b)))
'(_ A B))
(== (subst-if-not '_ (lambda (x)
(cl:if (consp x)
x
(symbol-package x)))
(_-to-gensym '((_ . a)
(_ . b)
(_ . c))))
'((_ . A) (_ . B) (_ . C))))
(defmacro let (var val &body body)
"The let statement sets the variable var to the value within the
scope of the body. Outside the let statement, any existing value
of var is unaffected. Let is like with but with a single variable
binding."
(cl:if (consp var)
(cl:let ((tem (gensym "let-"))
(var (_-to-gensym var)))
`((CL:LAMBDA (&REST ,tem)
(cl:DECLARE (cl:DYNAMIC-EXTENT ,tem))
(DESTRUCTURING-BIND (,var) ,tem
(cl:DECLARE (cl:IGNORABLE ,@(+internal-flatten `(,var))))
,@body ))
,val))
`(CL:LET ((,var ,val))
,@body)))
(defmacro leto (var val &body body)
`(cl:let ((,var ,val))
(w/obcall (,var)
,@body)))
(defmacro with (binds &body body)
"Creates a new variable binding and executes the body. The values
are computed before any of the assignments are done (like
Scheme's let, rather than let*). If the last variable doesn't
have a value, it is assigned nil."
(cl:loop :for x :on binds :by #'cddr
:collect (first x) :into vars
:collect (second x) :into vals
:finally (return
`(DESTRUCTURING-BIND ,vars (list ,@vals)
(cl:DECLARE (cl:IGNORABLE ,@(+internal-flatten vars)))
,@body))))
(defmacro witho (binds &body body)
(cl:loop :for x :on binds :by #'cddr
:collect (first x) :into vars
:collect (second x) :into vals
:finally (return
`(DESTRUCTURING-BIND ,vars (list ,@vals)
(cl:DECLARE (cl:IGNORABLE ,@(+internal-flatten vars)))
(w/obcall (,@vars)
,@body)))))
(defmacro withs (binds &body body)
"Creates a new variable binding and executes the body. The values
are computed sequentially (like Scheme's let*, rather than
let). If the last variable doesn't have a value, it is assigned
nil."
(cl:let ((binds (cl:loop :for vv :on binds :by #'cddr
:collect `(,(car vv) ,(cadr vv)))))
(cl:reduce (lambda (vv res) `(arc::let ,@vv ,res))
binds
:initial-value `(progn ,@body)
:from-end 'T)))
(defmacro withos (binds &body body)
(cl:let ((binds (cl:loop :for vv :on binds :by #'cddr
:collect `(,(car vv) ,(cadr vv)))))
(cl:reduce (lambda (vv res) `(arc::let ,@vv ,res))
binds
:initial-value `(w/obcall (,@(mapcar #'car binds))
,@body)
:from-end 'T)))
(tst withs
(== (withs (a 1 b (+ a 1)) (+ a b))
3)
(== (withs (a 1 b 2 (c d) '(3 4)) (+ a b c d))
10))
(defmacro do (&body forms)
`(progn ,@forms))
(defun map (fn seq &rest more-seqs)
"Applies f to the elements of the sequences, taking the first from each,
the second from each, and so on. If there are n sequences, f must be a function
accepting n arguments. The sequences can be lists or strings. If any sequence is
a string, then f must return a character, and the result will be a string made
up of the results from f. Otherwise, the result will be a list of the results
from f. The sequences are processed up to the length of the shortest sequence.
For a single list, map is the same as map1."
(cl:apply #'cl:map (cl:type-of seq)
fn
seq
more-seqs))
(tst map
(== (map (fn (a b c) (+ (* a 100) (* b 10) c))
'(1 2 3) '(4 5 6) '(7 8 9 10))
'(147 258 369))
(== (map (fn (_) (list _ (* _ 10))) '(1 2 3))
'((1 10) (2 20) (3 30)))
(== (map #'cdr '((1) (2 3) (4 5)))
'(nil (3) (5)))
(== (map (fn (c n) (coerce (+ n (coerce c 'int)) 'char)) "abc" '(0 2 4))
"adg")
(== (map #'min "bird" "elephant")
"bied"))
#|(mac do1 args
(w/uniq g
`(let ,g ,(car args)
,@(cdr args)
,g)))|#
#|(defmacro compose (&rest args)
(let g (uniq "compose-arg-")
`(cl:lambda (&rest ,g)
(cl:declare (cl:dynamic-extent ,g))
,(funcall
(labels ((self (fs)
(cl:if (cdr fs)
(list 'cl:funcall (car fs) (self (cdr fs)))
`(apply ,(if (car fs) (car fs) 'idfn) ,g))))
#'self)
args))))|#
(defmacro compose (&rest args)
(cl:let ((g (uniq "compose-arg-")))
`(cl:lambda (&rest ,g)
(cl:declare (cl:dynamic-extent ,g))
,(funcall
(labels ((self (fs)
(cl:if (cdr fs)
(typecase (car fs)
((cons (eql :local) (cons * null))
(list 'cl:funcall (cadar fs) (self (cdr fs))))
(T (list (car fs) (self (cdr fs)))))
(typecase (car fs)
((cons (eql :local) (cons * null))
`(apply ,(if (cadar fs) (cadar fs) 'idfn) ,g))
(T `(cl:let ((,(car fs) #',(car fs)))
(apply ,(if (car fs) (car fs) 'idfn) ,g)))))))
#'self)
args))))
#|(defmacro compose (&rest args)
(let ((g (gensym)))
`(fn ,g
,((afn (fs)
(if (cdr fs)
(list (car fs) (self (cdr fs)))
`(apply ,(if (car fs) (car fs) 'idfn) ,g)))
args))))|#
(defalias do1 cl:prog1
"Saves the first expression and returns it after executing the body.")
(tst do1
(== (let x 42 (do1 x (= x 50)))
42))
(defun sym (x) (coerce x 'sym))
(defun %pair (xs &optional (f #'list))
(cl:cond ((null xs) nil)
((null (cdr xs)) (list (list (car xs))))
(T (cons (funcall f (car xs) (cadr xs))
(%pair (cddr xs) f)))))
;;; eof