-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign2.scm
524 lines (452 loc) · 11.8 KB
/
assign2.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
(define (author)
(println "AUTHOR: Thomas Willingham twillingham@crimson.ua.edu")
)
(define (exprTest # $expr target)
(define result (catch (eval $expr #)))
(if (error? result)
(println $expr " is EXCEPTION: " (result 'value)
" (it should be " target ")")
(println $expr " is " result
" (it should be " target ")")
)
)
;;; un
(define (iterate # $x items $)
(define y (eval (list 'lambda (list $x) (cons 'begin $)) #))
(map y items)
)
(define (run1)
(iterate i (list 1 2 3 4)
(inspect i)
(inspect (* i i))
)
)
;;; deaux
(define (peval f @)
(define at @)
(define (listIt x y z)
(cond
((and (null? x) (null? y))
z
)
((null? x)
(listIt x (cdr y) (cons (car y) z))
)
((equal? (car x) 'MISSING)
(listIt (cdr x) (cdr y) (cons (car y) z))
)
(else
(listIt (cdr x) y (cons (car x) z))
)
)
)
(lambda (@) (apply f (reverse (listIt at @ ()))))
)
(define (run2)
(define a 1)
(define b 2)
(define c 3)
(define . 'MISSING)
(define (f x y z) (+ x y z))
(exprTest (f a b c) 6)
(exprTest ((peval f . . .) a b c) 6)
(exprTest ((peval f . b .) a c) 6)
(exprTest ((peval f . . c) a b) 6)
(exprTest ((peval f a . c) b) 6)
(exprTest ((peval f a b c)) 6)
)
;;; trois
(define (Stack)
(list (list ) 0)
)
(define (push s x)
(if (null? s)
(list (list x) (+ (cadr x) 1))
(list (cons x (car s)) (+ (cadr s) 1))
)
)
(define (pop s)
(if (equal? (ssize s) 0)
s
(list (cdr (car s)) (- (cadr s) 1))
)
)
(define (speek s)
(if (null? s)
s
(caar s)
)
)
(define (ssize s)
(cadr s)
)
(define (Queue)
(list (Stack) (Stack))
)
(define (enqueue q x)
(list (car q) (push (cadr q) x))
)
(define (dequeue q)
(define (moveAll r)
(if (equal? (ssize (cadr r)) 0)
r
(moveAll (list (push (car r) (speek (cadr r))) (pop (cadr r))))
)
)
(define (getDequeue r)
(define r (moveAll r))
(list (pop (car r)) (cadr r))
)
(if (equal? (ssize (car q)) 0)
(getDequeue q)
(list (pop (car q)) (cadr q))
)
)
(define (qpeek q)
(define (moveAll r)
(if (equal? (ssize (cadr r)) 0)
r
(moveAll (list (push (car r) (speek (cadr r))) (pop (cadr r))))
)
)
(if (equal? (ssize (car q)) 0)
(speek (car (moveAll q)))
(speek (car q))
)
)
(define (qsize q)
(+ (ssize (car q)) (ssize (cadr q)))
)
(define (run3)
(define (loop stack queue)
(define x (readInt))
(if (eof?)
(list stack queue)
(loop (push stack x) (enqueue queue x))
)
)
(define (popper s)
(cond
((!= (ssize s) 0)
(inspect (speek s))
(popper (pop s))
)
)
)
(define (dequeuer q)
(cond
((!= (qsize q) 0)
(inspect (qpeek q))
(dequeuer (dequeue q))
)
)
)
(define oldstream (setPort (open "data.ints" 'read)))
(define data (loop (Stack) (Queue)))
(popper (car data))
(dequeuer (cadr data))
(setPort oldstream)
(println "\n\n new queue\n")
(define q (dequeue (enqueue (dequeue (enqueue (enqueue (Queue) 1) 2)) 3)))
(exprTest (qpeek q) 3)
(define q (enqueue q 4))
(exprTest q "(( 0) ((4 3) 2))")
)
;;; quatre
(define (no-locals orig)
(define (noLocalsIt func args cur)
(if (and (list? (car cur)) (== (car (car cur)) 'define))
(if (== (length func) 0)
(if (== (length args) 0)
(noLocalsIt (list (cadr (car cur))) (list (caddr (car cur))) (cdr cur))
(noLocalsIt (list (cadr (car cur))) (append args (list (caddr (car cur)))) (cdr cur))
)
)
(append (list (list 'lambda func (car cur))) args)
)
)
(list (car orig) (cadr orig) (noLocalsIt '() '() (cddr orig)))
)
(define (run4)
(exprTest (no-locals (quote (define (nsq a) (define x (+ a 1)) (* x x)))) '(define (nsq a) ((lambda (x) (* x x)) (+ a 1))))
)
;;; cinq
(define pred
(lambda (n)
(lambda (f)
(lambda (x)
(((n (lambda (g)
(lambda (h)
(h (g f))
)
))
(lambda (u) x))
(lambda (u) u)
)
)
)
)
)
(define (run5)
(define four (lambda (f) (lambda (x) (f (f (f (f x)))))))
(inspect (define three (pred four)))
(exprTest ((three (lambda (z) (+ 1 z))) 0) 3)
(inspect (define two (pred three)))
(exprTest ((two (lambda (z) (+ 1 z))) 0) 2)
)
;;; six
(define (treeNode value left right)
(list value left right)
)
(define (treeflatten t)
(define (treeIter t depth)
(define left (cadr t))
(define right (caddr t))
(define val (car t))
(cond
((and (null? left) (null? right))
(list (list depth val)))
((null? left)
(treeIter right (+ depth 1)))
((null? right)
(treeIter left (+ depth 1)))
(else
(define lh (treeIter left (+ depth 1)))
(define rh (treeIter right (+ depth 1)))
(append rh lh)
)
)
)
(treeIter t 0)
)
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (treedepth t)
(define l (treeflatten t))
(define len (length l))
(define lam (lambda (x) (car x)))
(define m (map lam l))
(define a (accumulate + 0 m))
(/ a len)
)
(define (run6)
(define n7 (treeNode 7 nil nil))
(define n6 (treeNode 6 nil nil))
(define n5 (treeNode 5 n7 n7))
(define n4 (treeNode 4 n7 n7))
(define n3 (treeNode 3 n6 n5))
(define n2 (treeNode 2 n4 n3))
(define n1 (treeNode 1 n2 n3))
(inspect (define l (treeflatten n1)))
(inspect (define len (length l)))
(inspect (define lam (lambda (x) (car x))))
(inspect (define m (map lam l)))
(inspect (define a (accumulate + 0 m)))
(inspect (/ a len))
(exprTest (treedepth n1) 3)
)
;;; sept
(define (queens size)
(define (newPosition r c)
(cons r c))
(define (getRow pos)
(car pos))
(define (getCol pos)
(cdr pos))
(define newBoard nil)
(define (filter pred lst)
(cond ((null? lst) nil)
((pred (car lst))
(cons (car lst) (filter pred (cdr lst))))
(else (filter pred (cdr lst)))))
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (flatmap proc seq)
(accumulate append nil (map proc seq)))
(define (enumerate-interval low high)
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high))))
(define (addPosition row col positions)
(append (list (newPosition row col)) positions))
(define (safe? col positions)
(let (
(cur
(car positions ))
(others
(filter (lambda (q) (not (= col (getRow q)))) positions)))
(define (attacks? q1 q2)
(or (= (getCol q1) (getCol q2))
(= (abs (- (getRow q1) (getRow q2)))
(abs (- (getCol q1) (getCol q2))))))
(define (iter q board)
(or (null? board)
(and (not (attacks? q (car board)))
(iter q (cdr board))
)
)
)
(iter cur others))
)
(define (getPostitions n)
(if (= n -1)
(list newBoard)
(filter
(lambda (positions) (safe? n positions))
(flatmap
(lambda (otherQueens)
(map (lambda (newRow)
(addPosition n newRow otherQueens))
(enumerate-interval 0 (- size 1))
)
)
(getPostitions (- n 1))
)
)
)
)
(getPostitions (- size 1))
)
(define (run7)
(exprTest (queens 1) "(((0 . 0)))")
(exprTest (queens 2) "nil")
(exprTest (queens 3) "nil")
(exprTest (queens 4) "(((3 . 2) (2 . 0) (1 . 3) (0 . 1)) ((3 . 1) (2 . 3) (1 . 0) (0 . 2)))")
)
;;; huit
(define (cxr # x)
(define (cxrIter s)
(if (equal? s "")
'x
(if (equal? (car s) "a")
(cons 'car (list (cxrIter (cdr s))))
(cons 'cdr (list (cxrIter (cdr s))))
)
)
)
(eval (list 'lambda '(x) (cxrIter (string x))) #)
)
(define (run8)
(exprTest ((cxr 'adadd) (cons 1 (cons 2 (cons (cons 3 (cons 4 5)) 6)))) 4)
)
;;; neuf
(include "table.scm")
(define old+ +)
(define old- -)
(define old* *)
(define old/ /)
(define (install-generic)
(clearTable)
(set! + (lambda (a b) (apply-generic '+ a b)))
(set! - (lambda (a b) (apply-generic '- a b)))
(set! * (lambda (a b) (apply-generic '* a b)))
(set! / (lambda (a b) (apply-generic '/ a b)))
(putTable '+ '(STRING STRING) addStrings)
(putTable '+ '(STRING INTEGER) addStringInteger)
(putTable '+ '(INTEGER STRING) addIntegerString)
(putTable '- '(INTEGER STRING) subIntegerString)
(putTable '- '(STRING INTEGER) subStringInteger)
(putTable '* '(STRING INTEGER) mulStringInteger)
(putTable '* '(INTEGER STRING) mulIntegerString)
(putTable '/ '(INTEGER STRING) divIntegerString)
'generic-system-installed
)
(define (addStrings s1 s2)
(string+ s1 s2)
)
(define (addStringInteger s i)
(string+ s (string i))
)
(define (addIntegerString i s)
(+ i (int s))
)
(define (subIntegerString i s)
(- i (int s))
)
(define (subStringInteger s i)
(if (= 0 i)
s
(subStringInteger (cdr s) (- i 1))
)
)
(define (mulStringInteger s i)
(define (it x n)
(if (= n 0)
x
(it (string+ x s) (- n 1))
)
)
(if (= 0 i)
""
(it "" i)
)
)
(define (mulIntegerString i s)
(* i (int s))
)
(define (divIntegerString i s)
(/ i (int s))
)
(define (uninstall-generic)
(set! + old+)
(set! - old-)
(set! * old*)
(set! / old/)
'generic-system-uninstalled
)
(define (apply-generic op x y)
(define args (list x y))
(define proc (getTable op (map type args)))
(if (not (null? proc))
(apply proc args)
(cond
((equal? op '+)
(apply old+ args))
((equal? op '-)
(apply old- args))
((equal? op '*)
(apply old* args))
((equal? op '/)
(apply old/ args))
)
)
)
(define (run9)
(install-generic)
(inspect (+ "x" "y"))
(inspect (+ "123" 4))
(inspect (+ 123 "4"))
(inspect (- 123 "4"))
(inspect (- "abc" 1))
(inspect (* "abc" 3))
(inspect (* 3 "33"))
(inspect (/ 8 "2"))
(uninstall-generic)
)
;;; dix
(define (install-coercion)
(clearTable)
(putTable 'coerce '(REAL) real)
(putTable 'coerce '(STRING) string)
(putTable 'coerce '(INTEGER) int)
'generic-system-installed
)
(define (coerce n x)
(define proc (getTable 'coerce (list x)))
(if (not (null? proc))
(apply proc (list n))
)
)
(define (run10)
(install-coercion)
(exprTest (coerce "123.4" 'INTEGER) 123)
(exprTest (coerce '(1 (2.2) ((3 4) "5")) 'STRING) '(1 (2.2) ((3 4) "5")))
(exprTest (type (coerce '(1 (2.2) ((3 4) "5")) 'STRING)) "STRING")
)
(println "assignment 2 loaded!")