-
Notifications
You must be signed in to change notification settings - Fork 0
/
the little lisp.scm
368 lines (307 loc) · 9.98 KB
/
the little lisp.scm
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
(define nil #f)
(define false #f)
(define true #t)
(define atom?
(lambda (x)
(and (not (pair? x))(not (null? x)))))
(define lat?
(lambda (l)
(cond ( (null? l) true)
( else (and (atom? (car l))(lat? (cdr l)))))))
(define member?
(lambda ( a lat)
(cond ( (null? lat) nil)
( else (or (eq? a (car lat))(member? a (cdr lat)))))))
(define rember
(lambda ( a lat)
(cond ( (null? lat) '())
( (eq? a (car lat))(cdr lat))
( else (cons (car lat)(rember a (cdr lat)))))))
(define firsts
(lambda (lat)
(cond ( (null? lat) '())
(else (cons (car (car lat))(firsts (cdr lat)))))))
(define insertR
(lambda (new old lat)
(cond ( (null? lat) '())
( (eq? (car lat) old) (cons (car lat) (cons new (cdr lat))))
(else (cons (car lat)(insertR new old (cdr lat)))))))
(define insertL
(lambda (new old lat)
(cond ( (null? lat) '())
( (eq? (car lat) old)(cons new lat))
(else (cons (car lat)(insertL new old (cdr lat)))))))
(define subst
(lambda (new old lat)
(cond ( (null? lat) '())
( (eq? (car lat) old)(cons new (cdr lat)))
(else (cons (car lat)(subst new old (cdr lat)))))))
(define subst2
(lambda (new o1 o2 lat)
(cond ( (null? lat) '())
( (or (eq? (car lat) o1) (eq? (car lat) o2))(cons new (cdr lat)))
(else (cons (car lat)(subst2 new o1 o2 (cdr lat)))))))
(define multirember
(lambda (a lat)
(cond ( (null? lat) '())
( (eq? (car lat) a)(multirember a (cdr lat)))
(else (cons (car lat)(multirember a (cdr lat)))))))
(define multiinsertR
(lambda (new old lat)
(cond ( (null? lat) '())
( (eq? old (car lat))(cons (car lat) (cons new ( multiinsertR new old (cdr lat)))))
( else (cons (car lat) (multiinsertR new old (cdr lat)))))))
(define multiinsertL
(lambda (new old lat)
(cond ( (null? lat) '())
( (eq? (car lat) old)(cons new (cons (car lat) (multiinsertL new old (cdr lat)))))
(else (cons (car lat)(multiinsertL new old (cdr lat)))))))
(define multisubst
(lambda (new old lat)
(cond ( (null? lat) '())
( (eq? (car lat) old)(cons new (multisubst new old (cdr lat))))
(else (cons (car lat)(multisubst new old (cdr lat)))))))
(define add1 (lambda (x) ( x 1)))
(define sub1 (lambda (x) (- x 1)))
(define O
(lambda (m n)
(cond ((zero? m) n)
(else (add1 (O n (sub1 m)))))))
(define O-
(lambda (m n)
(cond ((zero? m) n)
(else (sub1 (O- n (sub1 m)))))))
(define addtup
(lambda (tup)
(cond ( (null? tup) 0)
(else ( (car tup) (addtup (cdr tup)))))))
(define O*
(lambda (m n)
(cond ( (zero? m) 0)
(else (O n (O* n (sub1 m)))))))
(define tup
(lambda (tup1 tup2)
(cond
( (null? tup1) tup2)
( (null? tup2) tup1)
(else (cons (O (car tup1)(car tup2)) (tup (cdr tup1 )(cdr tup2)))))))
(define O>
(lambda (n m)
(cond (( zero? n) #f)
(( zero? m) #t)
(else (O> (sub1 n)(sub1 m))))))
(define O<
(lambda (n m)
(cond (( zero? m) #f)
(( zero? n) #t)
(else (O< (sub1 n)(sub1 m))))))
(define O=
(lambda ( n m )
(cond ((zero? m)(zero? n))
((zero? n) #f)
(else (O= (sub1 n)(sub1 m))))))
(define O^
(lambda (a b)
(cond ( (zero? b) 1)
(else (O* a (O^ a (sub1 b)))))))
(define len
(lambda (lat)
(cond ( (null? lat) 0)
(else ( 1 (len (cdr lat)))))))
(define pick
(lambda ( n lat)
(cond ( (= n 1) (car lat))
(else (pick (- n 1) (cdr lat))))))
(define rempick
(lambda (n lat)
(cond ( (= n 1)(cdr lat))
(else (cons (car lat) (rempick (- n 1) (cdr lat)))))))
(define no-nums
(lambda (lat)
(cond ( (null? lat) '())
( (number? (car lat))(no-nums (cdr lat)))
(else (cons (car lat)(no-nums (cdr lat)))))))
(define all-nums
(lambda (lat)
(cond ( (null? lat) '())
( (number? (car lat))(cons (car lat)(all-nums (cdr lat))))
(else (all-nums (cdr lat))))))
(define eqan?
(lambda (a1 a2)
(cond ( (and (number? a1)(number? a2))(= a1 a2))
( (or (number? a1)(number? a2)) #f)
(else (eq? a1 a2)))))
(define occur
(lambda (a lat)
(cond ( (null? lat) 0)
( (eqan? a (car lat))( 1 (occur a (cdr lat))))
(else (occur a (cdr lat))))))
(define rember*
(lambda (a lat)
(cond ( (null? lat) '())
( (atom? (car lat))
(cond ( (eq? (car lat) a ) (rember* a (cdr lat)))
(else (cons (car lat)(rember* a (cdr lat))))))
(else (cons (rember* a (car lat))(rember* a (cdr lat)))))))
(define insertR*
(lambda (new old lat)
(cond ( (null? lat) '())
( (atom? (car lat))
(cond ( (eq? (car lat) old)(cons (car lat) (cons new (insertR* new old (cdr lat)))))
( else (cons (car lat )(insertR* new old (cdr lat))))))
(else (cons (insertR* new old (car lat))(insertR* new old (cdr lat)))))))
(define occur*
(lambda (a lat)
(cond ( (null? lat) 0)
( (atom? (car lat))
(cond ( (eq? (car lat) a)( 1 (occur* a (cdr lat))))
(else (occur* a (cdr lat)))))
(else ( (occur* a (car lat)) (occur* a (cdr lat)))))))
(define subst*
(lambda (new old lat)
(cond ( (null? lat) '())
( (atom? (car lat))
(cond (( eq? (car lat) old)(cons new (subst* new old (cdr lat))))
(else (cons (car lat)(subst* new old (cdr lat))))))
(else (cons (subst* new old (car lat)) (subst* new old (cdr lat)))))))
(define insertL*
(lambda (new old lat)
(cond ( (null? lat) '())
( (atom? (car lat))
( cond ( (eq? (car lat) old)(cons new (cons (car lat)(insertL* new old (cdr lat)))))
(else (cons (car lat)(insertL* new old (cdr lat))))))
(else (cons (insertL* new old (car lat))(insertL* new old (cdr lat)))))))
(define member*
(lambda (a lat)
(cond ( (null? lat) '())
( (atom? (car lat))
(cond ((eq? (car lat) a)(member* a (cdr lat)))
(else (cons (car lat)(member* a (cdr lat))))))
(else (cons (member* a (car lat))(member* a (cdr lat)))))))
(define leftmost
(lambda (lat)
(cond ( (atom? (car lat))(car lat))
(else (leftmost (car lat))))))
(define eqlist?
(lambda (lat1 lat2)
(cond ( (and (null? lat1)(null? lat2)) true)
( (or (null? lat1)(null? lat2)) false)
( (and (atom? (car lat1))(atom? (car lat2)))(and (eqan? (car lat1)(car lat2))(eqlist? (cdr lat1)(cdr lat2))))
( (or (atom? (car lat1))(atom? (car lat2))) false)
( else (and (eqlist? (car lat1)(car lat2))(eqlist? (cdr lat1)(cdr lat2)))))))
(define equal?*
(lambda (s1 s2)
(cond
(( and (atom? s1)(atom? s2))(eqan? s1 s2))
((atom? s1) #f)
((atom? s2) #f)
(else (eqlist? s1 s2)))))
(define numbered?
(lambda (aexp)
(cond
( (atom? aexp)(number? aexp))
(else
(and (numbered? (car aexp))(numbered? (car (cdr (cdr aexp)))))))))
(define 1st-sub-aexp
(lambda (aexp)
(car (cdr aexp))))
(define 2nd-sub-aexp
(lambda (aexp)
(car (cdr (cdr aexp)))))
(define operator
(lambda (aexp)
(car aexp)))
(define value
(lambda (aexp)
(cond ((atom? aexp) aexp)
((eq? (car aexp) ' )(O (value(1st-sub-aexp aexp))(value(2nd-sub-aexp aexp))))
((eq? (car aexp) '*)(O* (value(1st-sub-aexp aexp))(value(2nd-sub-aexp aexp))))
((eq? (car aexp) '^)(O^ (value(1st-sub-aexp aexp))(value(2nd-sub-aexp aexp)))))))
(define sero?
(lambda (n)
(null? n)))
(define edd1
(lambda (n)
(cons '() n)))
(define zub1
(lambda (n)
(cdr n)))
(define set?
(lambda (s)
(cond ( (null? s) true)
( (member? (car s) (cdr s))#f)
(else (set? (cdr s))))))
(define markset
(lambda (s)
(cond ( (null? s) '())
( (member? (car s) (cdr s))(markset (cdr s)))
(else (cons (car s)(markset (cdr s)))))))
(define subset
(lambda (set1 set2)
(cond ( (null? set1) #t)
( (member? (car set1) set2)(subset (cdr set1) set2))
(else #f))))
(define eqset?
(lambda (set1 set2)
(cond ( (and (null? set1)(null? set2)) #t)
( (and (subset set1 set2)(subset set2 set1))))))
(define interset?
(lambda (set1 set2)
(cond ( (or ( null? set1)(null? set2)) #f)
( (or (member? (car set1) set2)(interset? (cdr set1) set2))))))
(define interset
(lambda (set1 set2)
(cond ( ( null? set1) '())
( (member? (car set1) set2)(cons (car set1)(interset (cdr set1) set2)))
(else (interset (cdr set1 ) set2)))))
(define union
(lambda (set1 set2)
(cond ( (null? set1) set2)
( (member? (car set1) set2)(union (cdr set1) set2))
( else (cons (car set1)(union (cdr set1) set2))))))
(define intersectall
(lambda (l-set)
(cond ( (null? (cdr l-set)) (car l-set))
( else (interset (car l-set) (intersectall (cdr l-set)))))))
(define a-pair?
(lambda (x)
(cond ( (atom? x) #f)
( (null? x) #f)
( (null? (cdr x)) #f)
( (null? (cdr (cdr x))) #t)
(else #f))))
(define first
(lambda (s)
(car s)))
(define second
(lambda (s)
(car (cdr s))))
(define third
(lambda (s)
(car (cdr (cdr s)))))
(define fun
(lambda (rel)
(set? (firsts rel))))
(define build
(lambda (s1 s2)
(cons s1 (cons s2 '()))))
(define revrel
(lambda (rel)
(cond ( (null? rel) '())
(else (cons (build (second (car rel))(first (car rel)))(revrel (cdr rel)))))))
(define seconds
(lambda (lat)
(cond ( (null? lat) '())
(else (cons (second (car lat)) (seconds (cdr lat)))))))
(define fullfun?
(lambda (fun)
(set? (seconds fun))))
(define rember-f
(lambda (f a lat)
(cond ( (null? lat) '())
( (f (car lat) a)(rember-f f a (cdr lat)))
(else (cons (car lat)(rember-f f a (cdr lat)))))))
(define eq-c?
(lambda (x)
(lambda (a)
(eq? x a))))