-
Notifications
You must be signed in to change notification settings - Fork 2
/
a17-test-code.ss
466 lines (430 loc) · 11.5 KB
/
a17-test-code.ss
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
;; Test code for CSSE 304 Assignment 17
(define (test-set!-local-variables)
(let ([correct '(
73
93
19
8
43
)]
[answers
(list
(eval-one-exp '
(let ([f #f] [x 3])
(set! f (lambda (n)
(+ 3 (* n 10))))
(set! x 7)
(f x)))
(eval-one-exp '((lambda (x) (set! x (+ x 1)) (+ x 2)) 90))
(eval-one-exp '
(let ([x 5] [y 3])
(let ([z (begin (set! x (+ x y))
x)])
(+ z (+ x y)))))
(eval-one-exp '
(let ([a 5])
(if (not (= a 6))
(begin (set! a (+ 1 a))
(set! a (+ 1 a))) 3) (+ 1 a)))
(eval-one-exp '
(let ([f #f])
(let ([dummy (begin (set! f (lambda (n) (+ 3 (* n 10))))
3)])
(f 4))))
)])
(display-results correct answers equal?)))
(define (test-simple-defines)
(let ([correct '(
8
13
(12 14)
32
)]
[answers
(list
(eval-one-exp ' (begin (define a 5) (+ a 3)))
(eval-one-exp ' (begin (define c 5)
(define d (+ c 2))
(+ d (add1 c))))
(eval-one-exp '
(begin
(define e 5)
(let ([f (+ e 2)])
(set! e (+ e f))
(set! f (* 2 f))
(list e f))))
(eval-one-exp '
(begin (define ff
(letrec ([ff (lambda (x)
(if (= x 1)
2
(+ (* 2 x)
(ff (- x 2)))))])
ff))
(ff 7)))
)])
(display-results correct answers equal?)))
(define (test-letrec-and-define)
(let ([correct '(
55
773
)]
[answers
(list
(begin (reset-global-env)
(eval-one-exp '
(letrec ([f (lambda (n)
(if (= n 0)
0
(+ n
(f (sub1 n)))))])
(f 10))))
(eval-one-exp '
(letrec ([f (lambda (n)
(if (zero? n)
0
(+ 4 (g (sub1 n)))))]
[g (lambda (n)
(if (zero? n)
0
(+ 3 (f (sub1 n)))))])
(g (f (g (f 5))))))
)])
(display-results correct answers equal?)))
(define (test-named-let-and-define)
(let ([correct '(
120
120
(8 1 2 3 4 5 6 7)
987
16
(5 () (((4))) (3 2) 1)
)]
[answers
(list
(eval-one-exp '
(begin
(define fact
(lambda (n)
(let loop ((n n) (m 1))
(if (= n 0)
m
(loop (- n 1) (* m n))))))
(fact 5)))
(eval-one-exp '
(let fact ((n 5) (m 1))
(if (= n 0)
m
(fact (- n 1) (* m n)))))
(begin (reset-global-env)
(eval-one-exp '
(define rotate-linear
(letrec ([reverse
(lambda (lyst revlist)
(if (null? lyst)
revlist
(reverse (cdr lyst)
(cons (car lyst) revlist))))])
(lambda (los)
(let loop ([los los] [sofar '()])
(cond [(null? los) los]
[(null? (cdr los))
(cons (car los) (reverse sofar '()))]
[else (loop (cdr los) (cons (car los) sofar))]))))))
(eval-one-exp '(rotate-linear '(1 2 3 4 5 6 7 8))))
(begin (reset-global-env)
(eval-one-exp '
(define fib-memo
(let ([max 2] [sofar '((1 . 1) (0 . 1))])
(lambda (n)
(if (< n max)
(cdr (assq n sofar))
(let* ([v1 (fib-memo (- n 1))]
[v2 (fib-memo (- n 2))]
[v3 (+ v2 v1)])
(set! max (+ n 1))
(set! sofar (cons (cons n v3) sofar)) v3))))))
(eval-one-exp '(fib-memo 15)))
(begin (reset-global-env)
(eval-one-exp '
(define f1 (lambda (x)
(f2 (+ x 1)))))
(eval-one-exp '
(define f2 (lambda (x) (* x x))))
(eval-one-exp '(f1 3)))
(begin
(reset-global-env)
(eval-one-exp '
(define ns-list-recur
(lambda (seed item-proc list-proc)
(letrec ([helper (lambda (ls)
(if (null? ls)
seed
(let ([c (car ls)])
(if (or (pair? c)
(null? c))
(list-proc (helper c)
(helper (cdr ls)))
(item-proc c (helper (cdr ls)))))))])
helper))))
(eval-one-exp '
(define append
(lambda (s t)
(if (null? s)
t
(cons (car s)
(append (cdr s) t))))))
(eval-one-exp '
(define reverse*
(let ([snoc (lambda (x y)
(append y (list x)))])
(ns-list-recur '() snoc snoc))))
(eval-one-exp '(reverse* '(1 (2 3) (((4))) () 5)))))])
(display-results correct answers equal?)))
(define (test-set!-global-variables)
(let ([correct '(
7
4
120
9
)]
[answers
(list
(begin (reset-global-env)
(eval-one-exp '(define a 3))
(eval-one-exp '(set! a 7))
(eval-one-exp 'a))
(begin (reset-global-env)
(eval-one-exp '(define a 3))
(eval-one-exp '(define f '()))
(eval-one-exp '(set! f (lambda (x) (+ x 1))))
(eval-one-exp '(f a)))
(begin (reset-global-env)
(eval-one-exp '(define a 5))
(eval-one-exp '(define f '()))
(eval-one-exp '(set! f (lambda (x)
(if (= x 0)
1
(* x (f (- x 1)))))))
(eval-one-exp '(f a)))
(begin (reset-global-env)
(eval-one-exp '(define a 5))
(eval-one-exp '(let ([b 7]) (set! a 9)))
(eval-one-exp 'a))
)])
(display-results correct answers equal?)))
(define (test-order-matters!)
(let ([correct '(
(30 (29 27 24 20 15 9 2 3) 0)
55
)]
[answers
(list
(eval-one-exp '
(let ([r 2] [ls '(3)] [count 7])
(let loop ()
(if (> count 0)
(begin (set! ls (cons r ls))
(set! r (+ r count))
(set! count (- count 1)) (loop)) ))
(list r ls count)))
(eval-one-exp '
(begin
(define latest 1)
(define total 1)
(or (begin
(set! latest (+ latest 1)) ;l = 2
(set! total (+ total latest)) ;t = 3
(> total 50))
(begin (set! latest (+ latest 1)) ;l = 3
(set! total (+ total latest)) ;t = 6
(> total 50))
(begin (set! latest (+ latest 1)) ;l = 4
(set! total (+ total latest)) ;t = 10
(> total 50))
(begin (set! latest (+ latest 1)) ;l = 5
(set! total (+ total latest)) ;t = 15
(> total 50))
(begin (set! latest (+ latest 1)) ;l = 6
(set! total (+ total latest)) ;t = 21
(> total 50))
(begin (set! latest (+ latest 1)) ;l = 7
(set! total (+ total latest)) ;t = 28
(> total 50))
(begin (set! latest (+ latest 1)) ;l = 8
(set! total (+ total latest)) ;t = 36
(> total 50))
(begin (set! latest (+ latest 1)) ;l = 9
(set! total (+ total latest)) ;t = 45
(> total 50))
(begin (set! latest (+ latest 1)) ;l = 10
(set! total (+ total latest)) ;t = 55
(> total 50))
(begin (set! latest (+ latest 1)) ;l = 11
(set! total (+ total latest)) ;t = 66
(> total 50)))
total))
)])
(display-results correct answers equal?)))
(define (test-misc)
(let ([correct '(
3
(5 7)
)]
[answers
(list
(eval-one-exp '(apply apply (list + '(1 2))))
(eval-one-exp '(apply map (list (lambda (x) (+ x 3)) '(2 4))))
)])
(display-results correct answers equal?)))
(define (test-ref)
(let ([correct '(
(4 3)
(4 4)
(1 2 3)
((1 2 3)(b b b))
)]
[answers
(list
(eval-one-exp '
(let ([a 3]
[b 4]
[swap! (lambda ((ref x) (ref y))
(let ([temp x])
(set! x y)
(set! y temp)))])
(swap! a b)
(list a b)))
(eval-one-exp '
(let ([a 3]
[b 4]
[swap (lambda ((ref x) y)
(let ([temp x])
(set! x y)
(set! y temp)))])
(swap a b)
(list a b)))
(begin (reset-global-env)
(eval-one-exp '
(let* ([a '(1 2 3)]
[b ((lambda ((ref x)) x) a)])
(set! b 'foo) a)))
(begin (reset-global-env)
(eval-one-exp ' (define x '(a a a)))
(eval-one-exp '(define y '(b b b)))
(eval-one-exp '(let ()
((lambda ((ref x) y)
(set! x '(1 2 3))
(set! y '(4 5 6)))
x y)
(list x y))))
)])
(display-results correct answers equal?)))
;-----------------------------------------------
(define display-results
(lambda (correct results test-procedure?)
(display ": ")
(pretty-print
(if (andmap test-procedure? correct results)
'All-correct
`(correct: ,correct yours: ,results)))))
(define sequal?-grading
(lambda (l1 l2)
(cond
((null? l1) (null? l2))
((null? l2) (null? l1))
((or (not (set?-grading l1))
(not (set?-grading l2)))
#f)
((member (car l1) l2) (sequal?-grading
(cdr l1)
(rember-grading
(car l1)
l2)))
(else #f))))
(define set?-grading
(lambda (s)
(cond [(null? s) #t]
[(not (list? s)) #f]
[(member (car s) (cdr s)) #f]
[else (set?-grading (cdr s))])))
(define rember-grading
(lambda (a ls)
(cond
((null? ls) ls)
((equal? a (car ls)) (cdr ls))
(else (cons (car ls) (rember-grading a (cdr ls)))))))
(define set-equals? sequal?-grading)
(define find-edges ; e know that this node is in the graph before we do the call
(lambda (graph node)
(let loop ([graph graph])
(if (eq? (caar graph) node)
(cadar graph)
(loop (cdr graph))))))
;; Problem 8 graph?
(define set? ;; Is this list a set? If not, it is not a graph.
(lambda (list)
(if (null? list) ;; it's an empty set.
#t
(if (member (car list) (cdr list))
#f
(set? (cdr list))))))
(define graph?
(lambda (obj)
(and (list? obj)
(let ([syms (map car obj)])
(and (set? syms)
(andmap symbol? syms)
(andmap (lambda (x)
(andmap (lambda (y) (member y (remove (car x) syms)))
(cadr x)))
obj))))))
(define graph-equal?
(lambda (a b)
(and
(graph? a)
(graph? b)
(let ([a-nodes (map car a)]
[b-nodes (map car b)])
(and
(set-equals? a-nodes b-nodes)
; Now See if the edges from each node are equivalent in the two graphs.
(let loop ([a-nodes a-nodes])
(if (null? a-nodes)
#t
(let ([a-edges (find-edges a (car a-nodes))]
[b-edges (find-edges b (car a-nodes))])
(and (set-equals? a-edges b-edges)
(loop (cdr a-nodes)))))))))))
(define (test-graph-equal)
(list
(graph-equal? '((a (b)) (b (a))) '((b (a)) (a (b))))
(graph-equal? '((a (b c d)) (b (a c d)) (c (a b d)) (d (a b c)))
'((b (a c d)) (c (a b d)) (a (b d c)) (d (b a c))))
(graph-equal? '((a ())) '((a ())))
(graph-equal? '((a (b c)) (b (a c)) (c (a b))) '((a (b c)) (b (a c)) (c (a b))))
(graph-equal? '() '())
))
(define g test-graph-equal)
;You can run the tests individually, or run them all
;#by loading this file (and your solution) and typing (r)
(define (run-all)
(begin
(display 'set!-local-variables)
(test-set!-local-variables)
(display 'simple-defines)
(test-simple-defines)
(display 'letrec-and-define)
(test-letrec-and-define)
(display 'named-let-and-define)
(test-named-let-and-define)
(display 'set!-global-variables)
(test-set!-global-variables)
(display 'order-matters!)
(test-order-matters!)
(display 'misc)
(test-misc)
(display 'ref)
(test-ref))
)
(define r run-all)