-
Notifications
You must be signed in to change notification settings - Fork 2
/
A16-test-code.ss
278 lines (244 loc) · 6.84 KB
/
A16-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
;; Test code for CSSE 304 Assignment 16
(define (test-basics)
(let ([correct '(
(1 1 2 6 24 120)
40320
120
(#t #f #f #t)
)]
[answers
(list
(eval-one-exp '
(letrec ([fact (lambda (x)
(if (zero? x)
1
(* x (fact (- x 1)))))])
(map fact '(0 1 2 3 4 5))))
(eval-one-exp '
(let f ([n 8] [acc 1])
(if (= n 0)
acc
(f (sub1 n) (* acc n)))))
(eval-one-exp '
(let ([n 5])
(let f ([n n] [acc 1])
(if (= n 0)
acc
(f (sub1 n) (* acc n))))))
(eval-one-exp '
(letrec ([even? (lambda (n)
(if (zero? n)
#t
(odd? (- n 1))))]
[odd? (lambda (m)
(if (zero? m)
#f
(even? (- m 1))))])
(list (odd? 3) (even? 3) (odd? 4) (even? 4)))) )])
(display-results correct answers equal?)))
(define (test-answers-are-sets)
(let ([correct '(
(k e b d a c)
((3 a) (2 b)(3 b) (2 a) (1 a) (1 b))
)]
[answers
(list
(eval-one-exp '
(letrec ([union
(lambda (s1 s2)
(cond [(null? s1) s2]
[(member? (car s1) s2) (union (cdr s1) s2)]
[else (cons (car s1) (union (cdr s1) s2))]))]
[member? (lambda (sym ls)
(cond [(null? ls) #f]
[(eqv? (car ls) sym) #t]
[else (member? sym (cdr ls))]))])
(union '(a c e d k) '(e b a d c))))
(eval-one-exp '
(letrec ([product
(lambda (x y)
(if (null? y)
'()
(let loop ([x x] [accum '()])
(if (null? x)
accum
(loop (cdr x)
(append (map (lambda (s)
(list (car x) s))
y)
accum))))))])
(product '(1 2 3) '(a b))))
)])
(display-results correct answers sequal?-grading)))
(define (test-additional)
(let ([correct '(
(8 6 5 4 3 2 1)
)]
[answers
(list
(eval-one-exp '
(letrec ([sort (lambda (pred? l)
(if (null? l) l
(dosort pred? l (length l))))]
[merge (lambda (pred? l1 l2)
(cond [(null? l1) l2]
[(null? l2) l1]
[(pred? (car l2) (car l1))
(cons (car l2)
(merge pred? l1 (cdr l2)))]
[else (cons (car l1) (merge pred?
(cdr l1) l2))]))]
[dosort (lambda (pred? ls n)
(if (= n 1)
(list (car ls))
(let ([mid (quotient n 2)])
(merge pred? (dosort pred? ls mid)
(dosort pred?
(list-tail ls mid)
(- n mid))))))])
(sort > '(3 8 1 4 2 5 6))))
)])
(display-results correct answers equal?)))
(define (test-subst-leftmost)
(let ([correct '(
(((a b (c () (d new (f g)) h)) i))
)]
[answers
(list
(eval-one-exp '
(letrec (
[apply-continuation (lambda (k . list-of-values)
(apply k list-of-values))]
[subst-left-cps
(lambda (new old slist changed unchanged)
(let loop ([slist slist]
[changed changed]
[unchanged unchanged])
(cond
[(null? slist) (apply-continuation unchanged)]
[(symbol? (car slist))
(if (eq? (car slist) old)
(apply-continuation changed (cons new (cdr slist)))
(loop (cdr slist)
(lambda (changed-cdr)
(apply-continuation changed
(cons (car slist) changed-cdr)))
unchanged))]
[else
(loop (car slist)
(lambda (changed-car)
(apply-continuation changed
(cons changed-car (cdr slist))))
(lambda ()
(loop (cdr slist)
(lambda (changed-cdr)
(apply-continuation changed
(cons (car slist) changed-cdr)))
unchanged)))])))])
(let ([s '((a b (c () (d e (f g)) h)) i)])
(subst-left-cps 'new 'e s
(lambda (changed-s)
(subst-left-cps 'new 'q s
(lambda (wont-be-changed) 'whocares)
(lambda () (list changed-s))))
(lambda () "It's an error to get here"))))))])
(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)
(display 'basics)
(test-basics)
(display 'answers-are-sets)
(test-answers-are-sets)
(display 'additional)
(test-additional)
(display 'subst-leftmost)
(test-subst-leftmost)
)
(define r run-all)