-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys.lac
292 lines (229 loc) · 6.06 KB
/
sys.lac
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
;;; -*-Lisp-*-
;;; Rationale:
;;;
;;; The basic definitions, except where noted, are compatible with
;;; LISP 1.5.
;;; The evaluator itself is, though, differente, and this shows in
;;; the presence of LET and the absence of FUNCTION
;;;
;;; Definitions
;;;
(define defmacro
(macro (name binds &rest body) `(define ,name (macro ,binds ,@body))))
(defmacro defun (name binds &rest body)
`(define ,name (labels ,name , binds ,@body)))
(defmacro setq (x v) `(set ',x ,v))
(defun caar (x) (car (car x)))
(defun cadr (x) (car (cdr x)))
(defun cdar (x) (cdr (car x)))
(defun cddr (x) (cdr (cdr x)))
(defun caaar (x) (car (car (car x))))
(defun caadr (x) (car (car (cdr x))))
(defun cadar (x) (car (cdr (car x))))
(defun caddr (x) (car (cdr (cdr x))))
(defun cdaar (x) (cdr (car (car x))))
(defun cdadr (x) (cdr (car (cdr x))))
(defun cddar (x) (cdr (cdr (car x))))
(defun cdddr (x) (cdr (cdr (cdr x))))
(defmacro let (binds &rest body)
`((lambda ,(mapcar car binds) ,@body)
,@(mapcar cadr binds)))
;;;
;;; Logical Connectives
;;;
(defun not (x)
(cond (x NIL) (T)))
(defmacro or (&rest y)
(cond
(y `(cond (,(car y)) ((or ,@(cdr y)))))
(T 'NIL)))
(defmacro and (&rest y)
(cond
(y `(cond (,(car y) (and ,@(cdr y)))))
(T 'T)))
;;;
;;; Elementary Functions
;;;
;; car implicit
;; cdr implicit
;; cons implicit
(defun atom (x) (cond ((consp x) NIL)(T T)))
;; eq implicit
(defun equal (x y)
(cond
((atom x) (cond ((atom y) (atom-equal x y))))
((consp x)(cond ((consp y) (cond ((equal (car x) (car y))
(equal (cdr x) (cdr y)))))))))
(defun list (&rest y) y)
(defun null (x) (eq x NIL))
(defun listp (x) (or (null x) (consp x)))
;; rplaca implicit
;; rplacd implicit
;;;
;;; List Handling Functions
;;;
;;; N.B.: LISP 1.5 seem to support only two arguments
(defun append (&rest y)
(cond
((null y) NIL)
((null (caar y)) (apply append (cdr y)))
(t (cons (caar y)
(apply append (cons (cdar y) (cdr y)))))))
(defun assoc (x a)
(cond
((null a) NIL)
((equal (caar a) x) (car a))
(t (assoc x (cdr a)))))
(defun assq (x a)
(cond
((null a) NIL)
((eq (caar a) x) (car a))
(t (assq x (cdr a)))))
;; differ from clisp: (nconc 'X) error here.
(defun nconc (x y)
(cond ((null x) y)
(T (let ((f (labels f (z)
(cond ((cdr z) (f (cdr z)))
(T (rplacd z y))))))
(f x))
x)))
(defmacro conc (&rest y)
(cond
((null y) 'NIL)
(T `(nconc ,(car y) (conc ,@(cdr y))))))
(defun copy (x)
(cond
((null x) NIL)
((atom x) x)
(T (cons (copy (car x)) (copy (cdr x))))))
(defun reverse (l)
(let ((r) (f (labels f (u v)
(cond (u (f (cdr u) (cons (car u) v)))
(T v)))))
(f l NIL)))
(defun member (x l)
(cond
((null l) NIL)
((equal x (car l)) T)
(T (member x (cdr l)))))
(defun member (el lst)
(if (null lst)
NIL
(or (eq (car lst) el) (member el (cdr lst)))))
(defun length (lst)
(cond
((null lst) 0)
(T (+ 1 (length (cdr lst))))))
(defun efface (x l)
(cond
((null l) NIL)
((equal x (car l)) (cdr l))
(T (rplacd l (efface x (cdr l))))))
(defun subst (x y z)
(cond
((equal y z) x)
((atom z) z)
(t (cons (subst x y (car z))
(subst x y (cdr z))))))
;;;
;;; Functionals
;;; N.B.: Original LISP 1.5 (not MACLISP and subsequents) had the functional
;;; argument as last parameter.
;;;
(defun maplist (f x)
(cond
((null x) NIL)
(T (cons (f x) (maplist (cdr x) f)))))
(defun mapcon (f x)
(cond
((null x) NIL)
(T (nconc (f x) (maplist (cdr x) f)))))
(defun map (f x)
(cond
((null x) NIL)
(T (maplist (cdr x) f)))
NIL)
;; mapcar is embedded, for historical reasons
(defun search (x p f u)
(cond
((null x) (u x))
((p x) (f x))
(t (search (cdr x) p f u))))
;;;
;;; Arithmetic Functions
;;; N.B.: These should work both for fixnums and floats
;;;
(defun lessp (x y) (< x y))
(defun greaterp (x y) (> x y))
(defun zerop (x) (eq x 0))
(defun onep (x) (eq x 1))
(defun minusp (x) (< x 0))
(defun numberp (x) (integerp x))
;; fixp is integerp
(defun plus (&rest x)
(cond
((null x) 0)
(t (+ (car x) (apply plus (cdr x))))))
(defun difference (x y) (- x y))
(defun minus (x) -x)
(defun times (&rest x)
(cond
((null x) 1)
(t (* (car x) (apply times (cdr x))))))
(defun add1 (x) (+ x 1))
(defun sub1 (x) (- x 1))
(defun max (x &rest y)
(cond
((null y) x)
((<= x (car y)) (apply max y))
(t (apply max (cons x (cdr y))))))
(defun min (x &rest y)
(cond
((null y) x)
((>= x (car y)) (apply min y))
(t (apply min (cons x (cdr y))))))
(defun quotient (x y) (/ x y))
(defun remainder (x y) (% x y))
(defun divide (x y) (list (quotient x y) (remainder x y)))
(defun expt (x y)
(cond
((zerop y) 1)
(t (* x (expt x (sub1 y))))))
;;;
;;; Misc
;;; Mostly from Common Lisp
;;;
(defun first (l) (car l))
(defun second (l) (first (cdr l)))
(defun third (l) (second (cdr l)))
(defun fourth (l) (third (cdr l)))
(defun fifth (l) (fourth (cdr l)))
(defun sixth (l) (fifth (cdr l)))
(defun seventh (l) (sixth (cdr l)))
(defun eighth (l) (seventh (cdr l)))
(defun ninth (l) (eighth (cdr l)))
(defun tenth (l) (ninth (cdr l)))
(defun last (x) (cond ((null (cdr x)) x) (t (last (cdr x)))))
(defmacro if (test true false)
`(cond (,test ,true) (t ,false)))
(defun remove-if (test lst)
(if (null lst)
NIL
(if (test (car lst))
(remove-if test (cdr lst))
(cons (car lst) (remove-if test (cdr lst))))))
(defun remove-if-not (test lst)
(remove-if (lambda (x) (not (test x))) lst))
(defmacro push (x place)
(list 'set (list 'quote place) (list 'cons x place)))
(defun nthcdr (n l)
(cond ((eq n 0) l)
(t (nthcdr (- n 1) (cdr l)))))
(defun nth (n l)
(car (nthcdr n l)))
(defun 1+ (x) (+ 1 x))
(defun 1- (x) (- x 1))
(defun evenp (x) (eq (% x 2) 0))
(defun oddp (x) (eq (% x 2) 1))
(defmacro incf (x) `(setq ,x (1+ ,x)))
(defmacro decf (x) `(setq ,x (1- ,x)))