Blame


1 665c255d 2023-08-04 jrmu (define (ambeval exp env succeed fail)
2 665c255d 2023-08-04 jrmu ((analyze exp) env succeed fail))
3 665c255d 2023-08-04 jrmu (define (analyze exp)
4 665c255d 2023-08-04 jrmu (cond ((self-evaluating? exp)
5 665c255d 2023-08-04 jrmu (analyze-self-evaluating exp))
6 665c255d 2023-08-04 jrmu ((quoted? exp) (analyze-quoted exp))
7 665c255d 2023-08-04 jrmu ((variable? exp) (analyze-variable exp))
8 665c255d 2023-08-04 jrmu ((assignment? exp) (analyze-assignment exp))
9 665c255d 2023-08-04 jrmu ((permanent-assignment? exp)
10 665c255d 2023-08-04 jrmu (analyze-permanent-assignment exp))
11 665c255d 2023-08-04 jrmu ((definition? exp) (analyze-definition exp))
12 665c255d 2023-08-04 jrmu ((if? exp) (analyze-if exp))
13 665c255d 2023-08-04 jrmu ((and? exp) (analyze (and->if exp)))
14 665c255d 2023-08-04 jrmu ((or? exp) (analyze (or->if exp)))
15 665c255d 2023-08-04 jrmu ((not? exp) (analyze (not->if exp)))
16 665c255d 2023-08-04 jrmu ((xor? exp) (analyze (xor->or-and-not exp)))
17 665c255d 2023-08-04 jrmu ((lambda? exp) (analyze-lambda exp))
18 665c255d 2023-08-04 jrmu ((let? exp) (analyze (let->combination exp)))
19 665c255d 2023-08-04 jrmu ((let*? exp) (analyze (let*->nested-lets exp)))
20 665c255d 2023-08-04 jrmu ((named-let? exp) (analyze (named-let->combination exp)))
21 665c255d 2023-08-04 jrmu ((letrec? exp) (analyze (letrec->let exp)))
22 665c255d 2023-08-04 jrmu ((do? exp) (analyze (do->combination exp)))
23 665c255d 2023-08-04 jrmu ((begin? exp) (analyze-sequence (begin-actions exp)))
24 665c255d 2023-08-04 jrmu ((cond? exp) (analyze (cond->if exp)))
25 665c255d 2023-08-04 jrmu ((amb? exp) (analyze-amb exp))
26 665c255d 2023-08-04 jrmu ((ramb? exp) (analyze (ramb->amb exp)))
27 665c255d 2023-08-04 jrmu ((application? exp) (analyze-application exp))
28 665c255d 2023-08-04 jrmu (else
29 665c255d 2023-08-04 jrmu (error "Unknown expression type -- ANALYZE" exp))))
30 665c255d 2023-08-04 jrmu
31 665c255d 2023-08-04 jrmu
32 665c255d 2023-08-04 jrmu ;; analyzing procedures
33 665c255d 2023-08-04 jrmu (define (analyze-self-evaluating exp)
34 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
35 665c255d 2023-08-04 jrmu (succeed exp fail)))
36 665c255d 2023-08-04 jrmu (define (analyze-quoted exp)
37 665c255d 2023-08-04 jrmu (let ((qval (text-of-quotation exp)))
38 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
39 665c255d 2023-08-04 jrmu (succeed qval fail))))
40 665c255d 2023-08-04 jrmu (define (analyze-variable exp)
41 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
42 665c255d 2023-08-04 jrmu (succeed (lookup-variable-value exp env)
43 665c255d 2023-08-04 jrmu fail)))
44 665c255d 2023-08-04 jrmu (define (analyze-lambda exp)
45 665c255d 2023-08-04 jrmu (let ((vars (lambda-parameters exp))
46 665c255d 2023-08-04 jrmu (bproc (analyze-sequence (scan-out-defines (lambda-body exp)))))
47 665c255d 2023-08-04 jrmu ;; (bproc (analyze-sequence (lambda-body exp))))
48 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
49 665c255d 2023-08-04 jrmu (succeed (make-procedure vars bproc env)
50 665c255d 2023-08-04 jrmu fail))))
51 665c255d 2023-08-04 jrmu (define (analyze-if exp)
52 665c255d 2023-08-04 jrmu (let ((pproc (analyze (if-predicate exp)))
53 665c255d 2023-08-04 jrmu (cproc (analyze (if-consequent exp)))
54 665c255d 2023-08-04 jrmu (aproc (analyze (if-alternative exp))))
55 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
56 665c255d 2023-08-04 jrmu (pproc env
57 665c255d 2023-08-04 jrmu ;; success continuation for evaluating the predicate
58 665c255d 2023-08-04 jrmu ;; to obtain pred-value
59 665c255d 2023-08-04 jrmu (lambda (pred-value fail2)
60 665c255d 2023-08-04 jrmu (if (true? pred-value)
61 665c255d 2023-08-04 jrmu (cproc env succeed fail2)
62 665c255d 2023-08-04 jrmu (aproc env succeed fail2)))
63 665c255d 2023-08-04 jrmu ;; failure continuation for evaluating the predicate
64 665c255d 2023-08-04 jrmu fail))))
65 665c255d 2023-08-04 jrmu (define (analyze-sequence exps)
66 665c255d 2023-08-04 jrmu (define (sequentially a b)
67 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
68 665c255d 2023-08-04 jrmu (a env
69 665c255d 2023-08-04 jrmu ;; success continuation for calling a
70 665c255d 2023-08-04 jrmu (lambda (a-value fail2)
71 665c255d 2023-08-04 jrmu (b env succeed fail2))
72 665c255d 2023-08-04 jrmu ;; failure continuation for calling a
73 665c255d 2023-08-04 jrmu fail)))
74 665c255d 2023-08-04 jrmu (define (loop first-proc rest-procs)
75 665c255d 2023-08-04 jrmu (if (null? rest-procs)
76 665c255d 2023-08-04 jrmu first-proc
77 665c255d 2023-08-04 jrmu (loop (sequentially first-proc (car rest-procs))
78 665c255d 2023-08-04 jrmu (cdr rest-procs))))
79 665c255d 2023-08-04 jrmu (let ((procs (map analyze exps)))
80 665c255d 2023-08-04 jrmu (if (null? procs)
81 665c255d 2023-08-04 jrmu (error "Empty sequence -- ANALYZE"))
82 665c255d 2023-08-04 jrmu (loop (car procs) (cdr procs))))
83 665c255d 2023-08-04 jrmu (define (analyze-definition exp)
84 665c255d 2023-08-04 jrmu (let ((var (definition-variable exp))
85 665c255d 2023-08-04 jrmu (vproc (analyze (definition-value exp))))
86 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
87 665c255d 2023-08-04 jrmu (vproc env
88 665c255d 2023-08-04 jrmu (lambda (val fail2)
89 665c255d 2023-08-04 jrmu (define-variable! var val env)
90 665c255d 2023-08-04 jrmu (succeed 'ok fail2))
91 665c255d 2023-08-04 jrmu fail))))
92 665c255d 2023-08-04 jrmu (define (analyze-assignment exp)
93 665c255d 2023-08-04 jrmu (let ((var (assignment-variable exp))
94 665c255d 2023-08-04 jrmu (vproc (analyze (assignment-value exp))))
95 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
96 665c255d 2023-08-04 jrmu (vproc env
97 665c255d 2023-08-04 jrmu (lambda (val fail2) ; *1*
98 665c255d 2023-08-04 jrmu (let ((old-value
99 665c255d 2023-08-04 jrmu (lookup-variable-value var env)))
100 665c255d 2023-08-04 jrmu (set-variable-value! var val env)
101 665c255d 2023-08-04 jrmu (succeed 'ok
102 665c255d 2023-08-04 jrmu (lambda () ; *2*
103 665c255d 2023-08-04 jrmu (set-variable-value! var
104 665c255d 2023-08-04 jrmu old-value
105 665c255d 2023-08-04 jrmu env)
106 665c255d 2023-08-04 jrmu (fail2)))))
107 665c255d 2023-08-04 jrmu fail))))
108 665c255d 2023-08-04 jrmu (define (analyze-permanent-assignment exp)
109 665c255d 2023-08-04 jrmu (let ((var (permanent-assignment-variable exp))
110 665c255d 2023-08-04 jrmu (vproc (analyze (permanent-assignment-value exp))))
111 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
112 665c255d 2023-08-04 jrmu (vproc
113 665c255d 2023-08-04 jrmu env
114 665c255d 2023-08-04 jrmu (lambda (val val-fail)
115 665c255d 2023-08-04 jrmu (set-variable-value! var val env)
116 665c255d 2023-08-04 jrmu (succeed 'ok val-fail))
117 665c255d 2023-08-04 jrmu fail))))
118 665c255d 2023-08-04 jrmu
119 665c255d 2023-08-04 jrmu (define (analyze-application exp)
120 665c255d 2023-08-04 jrmu (let ((fproc (analyze (operator exp)))
121 665c255d 2023-08-04 jrmu (aprocs (map analyze (operands exp))))
122 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
123 665c255d 2023-08-04 jrmu (fproc env
124 665c255d 2023-08-04 jrmu (lambda (proc fail2)
125 665c255d 2023-08-04 jrmu (get-args aprocs
126 665c255d 2023-08-04 jrmu env
127 665c255d 2023-08-04 jrmu (lambda (args fail3)
128 665c255d 2023-08-04 jrmu (execute-application
129 665c255d 2023-08-04 jrmu proc args succeed fail3))
130 665c255d 2023-08-04 jrmu fail2))
131 665c255d 2023-08-04 jrmu fail))))
132 665c255d 2023-08-04 jrmu (define (get-args aprocs env succeed fail)
133 665c255d 2023-08-04 jrmu (if (null? aprocs)
134 665c255d 2023-08-04 jrmu (succeed '() fail)
135 665c255d 2023-08-04 jrmu ((car aprocs) env
136 665c255d 2023-08-04 jrmu ;; success continuation for this aproc
137 665c255d 2023-08-04 jrmu (lambda (arg fail2)
138 665c255d 2023-08-04 jrmu (get-args (cdr aprocs)
139 665c255d 2023-08-04 jrmu env
140 665c255d 2023-08-04 jrmu ;; success continuation for recursive
141 665c255d 2023-08-04 jrmu ;; call to get-args
142 665c255d 2023-08-04 jrmu (lambda (args fail3)
143 665c255d 2023-08-04 jrmu (succeed (cons arg args)
144 665c255d 2023-08-04 jrmu fail3))
145 665c255d 2023-08-04 jrmu fail2))
146 665c255d 2023-08-04 jrmu fail)))
147 665c255d 2023-08-04 jrmu
148 665c255d 2023-08-04 jrmu (define (analyze-amb exp)
149 665c255d 2023-08-04 jrmu (let ((cprocs (map analyze (amb-choices exp))))
150 665c255d 2023-08-04 jrmu (lambda (env succeed fail)
151 665c255d 2023-08-04 jrmu (define (try-next choices)
152 665c255d 2023-08-04 jrmu (if (null? choices)
153 665c255d 2023-08-04 jrmu (fail)
154 665c255d 2023-08-04 jrmu ((car choices) env
155 665c255d 2023-08-04 jrmu succeed
156 665c255d 2023-08-04 jrmu (lambda ()
157 665c255d 2023-08-04 jrmu (try-next (cdr choices))))))
158 665c255d 2023-08-04 jrmu (try-next cprocs))))
159 665c255d 2023-08-04 jrmu
160 665c255d 2023-08-04 jrmu
161 665c255d 2023-08-04 jrmu
162 665c255d 2023-08-04 jrmu
163 665c255d 2023-08-04 jrmu
164 665c255d 2023-08-04 jrmu
165 665c255d 2023-08-04 jrmu (define (tagged-list? exp tag)
166 665c255d 2023-08-04 jrmu (if (pair? exp)
167 665c255d 2023-08-04 jrmu (eq? (car exp) tag)
168 665c255d 2023-08-04 jrmu false))
169 665c255d 2023-08-04 jrmu
170 665c255d 2023-08-04 jrmu ;; amb/ramb
171 665c255d 2023-08-04 jrmu (define (amb? exp) (tagged-list? exp 'amb))
172 665c255d 2023-08-04 jrmu (define (amb-choices exp) (cdr exp))
173 665c255d 2023-08-04 jrmu (define (make-amb choices)
174 665c255d 2023-08-04 jrmu (cons 'amb choices))
175 665c255d 2023-08-04 jrmu
176 665c255d 2023-08-04 jrmu (define (ramb? exp)
177 665c255d 2023-08-04 jrmu (tagged-list? exp 'ramb))
178 665c255d 2023-08-04 jrmu (define (ramb->amb exp)
179 665c255d 2023-08-04 jrmu (make-amb (shuffle (amb-choices exp))))
180 665c255d 2023-08-04 jrmu
181 665c255d 2023-08-04 jrmu (define (shuffle items)
182 665c255d 2023-08-04 jrmu (if (null? items)
183 665c255d 2023-08-04 jrmu '()
184 665c255d 2023-08-04 jrmu (let ((first (list-ref items (random (length items)))))
185 665c255d 2023-08-04 jrmu (cons first
186 665c255d 2023-08-04 jrmu (shuffle (remove (lambda (i) (eq? first i))
187 665c255d 2023-08-04 jrmu items))))))
188 665c255d 2023-08-04 jrmu
189 665c255d 2023-08-04 jrmu
190 665c255d 2023-08-04 jrmu
191 665c255d 2023-08-04 jrmu ;; self-evaluating/variable/quoted
192 665c255d 2023-08-04 jrmu (define (self-evaluating? exp)
193 665c255d 2023-08-04 jrmu (cond ((number? exp) true)
194 665c255d 2023-08-04 jrmu ((string? exp) true)
195 665c255d 2023-08-04 jrmu (else false)))
196 665c255d 2023-08-04 jrmu (define (variable? exp) (symbol? exp))
197 665c255d 2023-08-04 jrmu (define (quoted? exp)
198 665c255d 2023-08-04 jrmu (tagged-list? exp 'quote))
199 665c255d 2023-08-04 jrmu (define (text-of-quotation exp) (cadr exp))
200 665c255d 2023-08-04 jrmu
201 665c255d 2023-08-04 jrmu ;; assignment/permanent-assignment/definition
202 665c255d 2023-08-04 jrmu (define (assignment? exp)
203 665c255d 2023-08-04 jrmu (tagged-list? exp 'set!))
204 665c255d 2023-08-04 jrmu (define (assignment-variable exp) (cadr exp))
205 665c255d 2023-08-04 jrmu (define (assignment-value exp) (caddr exp))
206 665c255d 2023-08-04 jrmu (define (make-assignment var val)
207 665c255d 2023-08-04 jrmu (list 'set! var val))
208 665c255d 2023-08-04 jrmu (define (permanent-assignment? exp)
209 665c255d 2023-08-04 jrmu (tagged-list? exp 'permanent-set!))
210 665c255d 2023-08-04 jrmu (define permanent-assignment-variable assignment-variable)
211 665c255d 2023-08-04 jrmu (define permanent-assignment-value assignment-value)
212 665c255d 2023-08-04 jrmu (define (definition? exp)
213 665c255d 2023-08-04 jrmu (tagged-list? exp 'define))
214 665c255d 2023-08-04 jrmu (define (definition-variable exp)
215 665c255d 2023-08-04 jrmu (if (symbol? (cadr exp))
216 665c255d 2023-08-04 jrmu (cadr exp)
217 665c255d 2023-08-04 jrmu (caadr exp)))
218 665c255d 2023-08-04 jrmu (define (definition-value exp)
219 665c255d 2023-08-04 jrmu (if (symbol? (cadr exp))
220 665c255d 2023-08-04 jrmu (caddr exp)
221 665c255d 2023-08-04 jrmu (make-lambda (cdadr exp) ; formal parameters
222 665c255d 2023-08-04 jrmu (cddr exp)))) ; body
223 665c255d 2023-08-04 jrmu (define (make-definition var val)
224 665c255d 2023-08-04 jrmu `(define ,var ,val))
225 665c255d 2023-08-04 jrmu
226 665c255d 2023-08-04 jrmu ;; if/and/or/not/xor
227 665c255d 2023-08-04 jrmu (define (if? exp) (tagged-list? exp 'if))
228 665c255d 2023-08-04 jrmu (define (if-predicate exp) (cadr exp))
229 665c255d 2023-08-04 jrmu (define (if-consequent exp) (caddr exp))
230 665c255d 2023-08-04 jrmu (define (if-alternative exp)
231 665c255d 2023-08-04 jrmu (if (not (null? (cdddr exp)))
232 665c255d 2023-08-04 jrmu (cadddr exp)
233 665c255d 2023-08-04 jrmu 'false))
234 665c255d 2023-08-04 jrmu (define (make-if predicate consequent alternative)
235 665c255d 2023-08-04 jrmu (list 'if predicate consequent alternative))
236 665c255d 2023-08-04 jrmu
237 665c255d 2023-08-04 jrmu (define (and? exp)
238 665c255d 2023-08-04 jrmu (tagged-list? exp 'and))
239 665c255d 2023-08-04 jrmu (define (and-clauses exp)
240 665c255d 2023-08-04 jrmu (cdr exp))
241 665c255d 2023-08-04 jrmu (define (or? exp)
242 665c255d 2023-08-04 jrmu (tagged-list? exp 'or))
243 665c255d 2023-08-04 jrmu (define (or-clauses exp)
244 665c255d 2023-08-04 jrmu (cdr exp))
245 665c255d 2023-08-04 jrmu (define (and->if exp)
246 665c255d 2023-08-04 jrmu (define (expand-clauses clauses)
247 665c255d 2023-08-04 jrmu (cond ((null? clauses) 'true)
248 665c255d 2023-08-04 jrmu ((null? (cdr clauses)) (car clauses))
249 665c255d 2023-08-04 jrmu (else (make-if (car clauses)
250 665c255d 2023-08-04 jrmu (expand-clauses (cdr clauses))
251 665c255d 2023-08-04 jrmu 'false))))
252 665c255d 2023-08-04 jrmu (expand-clauses (and-clauses exp)))
253 665c255d 2023-08-04 jrmu (define (or->if exp)
254 665c255d 2023-08-04 jrmu (define (expand-clauses clauses)
255 665c255d 2023-08-04 jrmu (if (null? clauses)
256 665c255d 2023-08-04 jrmu 'false
257 665c255d 2023-08-04 jrmu (make-if (car clauses)
258 665c255d 2023-08-04 jrmu (car clauses)
259 665c255d 2023-08-04 jrmu (expand-clauses (cdr clauses)))))
260 665c255d 2023-08-04 jrmu (expand-clauses (or-clauses exp)))
261 665c255d 2023-08-04 jrmu (define (not? exp)
262 665c255d 2023-08-04 jrmu (tagged-list? exp 'not))
263 665c255d 2023-08-04 jrmu (define (not->if exp)
264 665c255d 2023-08-04 jrmu `(if ,(cadr exp) false true))
265 665c255d 2023-08-04 jrmu (define (xor? exp)
266 665c255d 2023-08-04 jrmu (tagged-list? exp 'xor))
267 665c255d 2023-08-04 jrmu (define (xor->or-and-not exp)
268 665c255d 2023-08-04 jrmu (let ((pred-1 (cadr exp))
269 665c255d 2023-08-04 jrmu (pred-2 (caddr exp)))
270 665c255d 2023-08-04 jrmu `(or (and ,pred-1 (not ,pred-2))
271 665c255d 2023-08-04 jrmu (and (not ,pred-1) ,pred-2))))
272 665c255d 2023-08-04 jrmu
273 665c255d 2023-08-04 jrmu ;; lambda/let/let*/letrec
274 665c255d 2023-08-04 jrmu (define (lambda? exp) (tagged-list? exp 'lambda))
275 665c255d 2023-08-04 jrmu (define (lambda-parameters exp) (cadr exp))
276 665c255d 2023-08-04 jrmu (define (lambda-body exp) (cddr exp))
277 665c255d 2023-08-04 jrmu (define (make-lambda parameters body)
278 665c255d 2023-08-04 jrmu (cons 'lambda (cons parameters body)))
279 665c255d 2023-08-04 jrmu
280 665c255d 2023-08-04 jrmu (define (make-let vars vals body)
281 665c255d 2023-08-04 jrmu (cons 'let
282 665c255d 2023-08-04 jrmu (cons (map list vars vals)
283 665c255d 2023-08-04 jrmu body)))
284 665c255d 2023-08-04 jrmu (define (let? exp)
285 665c255d 2023-08-04 jrmu (and (tagged-list? exp 'let)
286 665c255d 2023-08-04 jrmu (not (symbol? (cadr exp)))))
287 665c255d 2023-08-04 jrmu (define (let-vars exp)
288 665c255d 2023-08-04 jrmu (map car (cadr exp)))
289 665c255d 2023-08-04 jrmu (define (let-vals exp)
290 665c255d 2023-08-04 jrmu (map cadr (cadr exp)))
291 665c255d 2023-08-04 jrmu (define (let-body exp)
292 665c255d 2023-08-04 jrmu (cddr exp))
293 665c255d 2023-08-04 jrmu (define (let->combination exp)
294 665c255d 2023-08-04 jrmu (make-application (make-lambda (let-vars exp) (let-body exp))
295 665c255d 2023-08-04 jrmu (let-vals exp)))
296 665c255d 2023-08-04 jrmu (define (named-let? exp)
297 665c255d 2023-08-04 jrmu (and (tagged-list? exp 'let)
298 665c255d 2023-08-04 jrmu v (symbol? (cadr exp))))
299 665c255d 2023-08-04 jrmu (define (named-let-name exp)
300 665c255d 2023-08-04 jrmu (cadr exp))
301 665c255d 2023-08-04 jrmu (define (named-let-vars exp)
302 665c255d 2023-08-04 jrmu (map car (caddr exp)))
303 665c255d 2023-08-04 jrmu (define (named-let-vals exp)
304 665c255d 2023-08-04 jrmu (map cadr (caddr exp)))
305 665c255d 2023-08-04 jrmu (define (named-let-body exp)
306 665c255d 2023-08-04 jrmu (cdddr exp))
307 665c255d 2023-08-04 jrmu (define (named-let->combination exp)
308 665c255d 2023-08-04 jrmu (sequence->exp
309 665c255d 2023-08-04 jrmu (list (make-definition (named-let-name exp)
310 665c255d 2023-08-04 jrmu (make-lambda (named-let-vars exp)
311 665c255d 2023-08-04 jrmu (named-let-body exp)))
312 665c255d 2023-08-04 jrmu (make-application (named-let-name exp)
313 665c255d 2023-08-04 jrmu (named-let-vals exp)))))
314 665c255d 2023-08-04 jrmu (define (make-named-let name vars vals body)
315 665c255d 2023-08-04 jrmu (cons 'let
316 665c255d 2023-08-04 jrmu (cons name
317 665c255d 2023-08-04 jrmu (cons (map list vars vals)
318 665c255d 2023-08-04 jrmu body))))
319 665c255d 2023-08-04 jrmu
320 665c255d 2023-08-04 jrmu (define (letrec? exp)
321 665c255d 2023-08-04 jrmu (tagged-list? exp 'letrec))
322 665c255d 2023-08-04 jrmu
323 665c255d 2023-08-04 jrmu (define (letrec-vars exp)
324 665c255d 2023-08-04 jrmu (map car (cadr exp)))
325 665c255d 2023-08-04 jrmu (define (letrec-vals exp)
326 665c255d 2023-08-04 jrmu (map cadr (cadr exp)))
327 665c255d 2023-08-04 jrmu (define (letrec-body exp)
328 665c255d 2023-08-04 jrmu (cddr exp))
329 665c255d 2023-08-04 jrmu (define (letrec->let exp)
330 665c255d 2023-08-04 jrmu (let* ((vars (letrec-vars exp))
331 665c255d 2023-08-04 jrmu (unassigneds (map (lambda (var) ''*unassigned*)
332 665c255d 2023-08-04 jrmu vars))
333 665c255d 2023-08-04 jrmu (vals (letrec-vals exp))
334 665c255d 2023-08-04 jrmu (assignments (map (lambda (var val)
335 665c255d 2023-08-04 jrmu (make-assignment var val))
336 665c255d 2023-08-04 jrmu vars
337 665c255d 2023-08-04 jrmu vals))
338 665c255d 2023-08-04 jrmu (body (letrec-body exp)))
339 665c255d 2023-08-04 jrmu (make-let vars
340 665c255d 2023-08-04 jrmu unassigneds
341 665c255d 2023-08-04 jrmu (append assignments body))))
342 665c255d 2023-08-04 jrmu
343 665c255d 2023-08-04 jrmu (define (make-application op args)
344 665c255d 2023-08-04 jrmu (cons op args))
345 665c255d 2023-08-04 jrmu
346 665c255d 2023-08-04 jrmu (define (let*? exp)
347 665c255d 2023-08-04 jrmu (tagged-list? exp 'let*))
348 665c255d 2023-08-04 jrmu (define let*-vars let-vars)
349 665c255d 2023-08-04 jrmu (define let*-vals let-vals)
350 665c255d 2023-08-04 jrmu (define let*-body let-body)
351 665c255d 2023-08-04 jrmu (define (let*->nested-lets exp)
352 665c255d 2023-08-04 jrmu (define (expand-lets vars vals)
353 665c255d 2023-08-04 jrmu (if (null? (cdr vars))
354 665c255d 2023-08-04 jrmu (make-let (list (car vars))
355 665c255d 2023-08-04 jrmu (list (car vals))
356 665c255d 2023-08-04 jrmu (let*-body exp))
357 665c255d 2023-08-04 jrmu (make-let (list (car vars))
358 665c255d 2023-08-04 jrmu (list (car vals))
359 665c255d 2023-08-04 jrmu (list (expand-lets (cdr vars) (cdr vals))))))
360 665c255d 2023-08-04 jrmu (let ((vars (let*-vars exp))
361 665c255d 2023-08-04 jrmu (vals (let*-vals exp)))
362 665c255d 2023-08-04 jrmu (if (null? vars)
363 665c255d 2023-08-04 jrmu (sequence->exp (let*-body exp))
364 665c255d 2023-08-04 jrmu (expand-lets vars vals))))
365 665c255d 2023-08-04 jrmu
366 665c255d 2023-08-04 jrmu ;; do loop
367 665c255d 2023-08-04 jrmu (define (do? exp)
368 665c255d 2023-08-04 jrmu (tagged-list? exp 'do))
369 665c255d 2023-08-04 jrmu (define (do-vars exp)
370 665c255d 2023-08-04 jrmu (map car (cadr exp)))
371 665c255d 2023-08-04 jrmu (define (do-inits exp)
372 665c255d 2023-08-04 jrmu (map cadr (cadr exp)))
373 665c255d 2023-08-04 jrmu (define (do-steps exp)
374 665c255d 2023-08-04 jrmu (map (lambda (var-init-step)
375 665c255d 2023-08-04 jrmu (if (null? (cddr var-init-step))
376 665c255d 2023-08-04 jrmu (car var-init-step)
377 665c255d 2023-08-04 jrmu (caddr var-init-step)))
378 665c255d 2023-08-04 jrmu (cadr exp)))
379 665c255d 2023-08-04 jrmu (define (do-test exp)
380 665c255d 2023-08-04 jrmu (caaddr exp))
381 665c255d 2023-08-04 jrmu (define (do-expressions exp)
382 665c255d 2023-08-04 jrmu (if (null? (cdaddr exp))
383 665c255d 2023-08-04 jrmu (caddr exp)
384 665c255d 2023-08-04 jrmu (cdaddr exp)))
385 665c255d 2023-08-04 jrmu (define (do-commands exp)
386 665c255d 2023-08-04 jrmu (cdddr exp))
387 665c255d 2023-08-04 jrmu (define (do->combination exp)
388 665c255d 2023-08-04 jrmu (make-named-let
389 665c255d 2023-08-04 jrmu 'do-iter
390 665c255d 2023-08-04 jrmu (do-vars exp)
391 665c255d 2023-08-04 jrmu (do-inits exp)
392 665c255d 2023-08-04 jrmu (list
393 665c255d 2023-08-04 jrmu (make-if
394 665c255d 2023-08-04 jrmu (do-test exp)
395 665c255d 2023-08-04 jrmu (sequence->exp (do-expressions exp))
396 665c255d 2023-08-04 jrmu (sequence->exp
397 665c255d 2023-08-04 jrmu (append (do-commands exp)
398 665c255d 2023-08-04 jrmu (list (make-application
399 665c255d 2023-08-04 jrmu 'do-iter
400 665c255d 2023-08-04 jrmu (do-steps exp)))))))))
401 665c255d 2023-08-04 jrmu
402 665c255d 2023-08-04 jrmu
403 665c255d 2023-08-04 jrmu ;; begin/sequence
404 665c255d 2023-08-04 jrmu (define (begin? exp) (tagged-list? exp 'begin))
405 665c255d 2023-08-04 jrmu (define (begin-actions exp) (cdr exp))
406 665c255d 2023-08-04 jrmu (define (last-exp? seq) (null? (cdr seq)))
407 665c255d 2023-08-04 jrmu (define (first-exp seq) (car seq))
408 665c255d 2023-08-04 jrmu (define (rest-exps seq) (cdr seq))
409 665c255d 2023-08-04 jrmu (define (sequence->exp seq)
410 665c255d 2023-08-04 jrmu (cond ((null? seq) seq)
411 665c255d 2023-08-04 jrmu ((last-exp? seq) (first-exp seq))
412 665c255d 2023-08-04 jrmu (else (make-begin seq))))
413 665c255d 2023-08-04 jrmu (define (make-begin seq) (cons 'begin seq))
414 665c255d 2023-08-04 jrmu
415 665c255d 2023-08-04 jrmu ;; application
416 665c255d 2023-08-04 jrmu (define (application? exp) (pair? exp))
417 665c255d 2023-08-04 jrmu (define (operator exp) (car exp))
418 665c255d 2023-08-04 jrmu (define (operands exp) (cdr exp))
419 665c255d 2023-08-04 jrmu (define (no-operands? ops) (null? ops))
420 665c255d 2023-08-04 jrmu (define (first-operand ops) (car ops))
421 665c255d 2023-08-04 jrmu (define (rest-operands ops) (cdr ops))
422 665c255d 2023-08-04 jrmu
423 665c255d 2023-08-04 jrmu ;; cond
424 665c255d 2023-08-04 jrmu (define (cond? exp) (tagged-list? exp 'cond))
425 665c255d 2023-08-04 jrmu (define (cond-clauses exp) (cdr exp))
426 665c255d 2023-08-04 jrmu (define (cond-else-clause? clause)
427 665c255d 2023-08-04 jrmu (eq? (cond-predicate clause) 'else))
428 665c255d 2023-08-04 jrmu (define (cond-predicate clause) (car clause))
429 665c255d 2023-08-04 jrmu (define (cond-actions clause) (cdr clause))
430 665c255d 2023-08-04 jrmu (define (cond-extended-clause? clause)
431 665c255d 2023-08-04 jrmu (and (not (null? (cdr clause))) (eq? (cadr clause) '=>)))
432 665c255d 2023-08-04 jrmu (define (cond-extended-proc clause)
433 665c255d 2023-08-04 jrmu (caddr clause))
434 665c255d 2023-08-04 jrmu (define (cond->if exp)
435 665c255d 2023-08-04 jrmu (expand-clauses (cond-clauses exp)))
436 665c255d 2023-08-04 jrmu (define (expand-clauses clauses)
437 665c255d 2023-08-04 jrmu (if (null? clauses)
438 665c255d 2023-08-04 jrmu 'false ; no else clause
439 665c255d 2023-08-04 jrmu (let ((first (car clauses))
440 665c255d 2023-08-04 jrmu (rest (cdr clauses)))
441 665c255d 2023-08-04 jrmu (if (cond-else-clause? first)
442 665c255d 2023-08-04 jrmu (if (null? rest)
443 665c255d 2023-08-04 jrmu (sequence->exp (cond-actions first))
444 665c255d 2023-08-04 jrmu (error "ELSE clause isn't last -- COND->IF"
445 665c255d 2023-08-04 jrmu clauses))
446 665c255d 2023-08-04 jrmu (if (cond-extended-clause? first)
447 665c255d 2023-08-04 jrmu (make-if (cond-predicate first)
448 665c255d 2023-08-04 jrmu (make-application
449 665c255d 2023-08-04 jrmu (cond-extended-proc first)
450 665c255d 2023-08-04 jrmu (list (cond-predicate first)))
451 665c255d 2023-08-04 jrmu (expand-clauses rest))
452 665c255d 2023-08-04 jrmu (make-if (cond-predicate first)
453 665c255d 2023-08-04 jrmu (sequence->exp (cond-actions first))
454 665c255d 2023-08-04 jrmu (expand-clauses rest)))))))
455 665c255d 2023-08-04 jrmu (define (true? x)
456 665c255d 2023-08-04 jrmu (not (eq? x false)))
457 665c255d 2023-08-04 jrmu (define (false? x)
458 665c255d 2023-08-04 jrmu (eq? x false))
459 665c255d 2023-08-04 jrmu
460 665c255d 2023-08-04 jrmu ;; procedure
461 665c255d 2023-08-04 jrmu (define (make-procedure parameters body env)
462 665c255d 2023-08-04 jrmu (list 'procedure parameters body env))
463 665c255d 2023-08-04 jrmu (define (scan-out-defines body)
464 665c255d 2023-08-04 jrmu (let* ((definitions (filter definition? body))
465 665c255d 2023-08-04 jrmu (vars (map definition-variable definitions))
466 665c255d 2023-08-04 jrmu (unassigneds (map (lambda (var) ''*unassigned*)
467 665c255d 2023-08-04 jrmu vars))
468 665c255d 2023-08-04 jrmu (vals (map definition-value definitions))
469 665c255d 2023-08-04 jrmu (assignments
470 665c255d 2023-08-04 jrmu (map (lambda (var val)
471 665c255d 2023-08-04 jrmu (make-assignment var val))
472 665c255d 2023-08-04 jrmu vars vals))
473 665c255d 2023-08-04 jrmu (exps (remove definition? body)))
474 665c255d 2023-08-04 jrmu (if (null? definitions)
475 665c255d 2023-08-04 jrmu body
476 665c255d 2023-08-04 jrmu (list
477 665c255d 2023-08-04 jrmu (make-let vars
478 665c255d 2023-08-04 jrmu unassigneds
479 665c255d 2023-08-04 jrmu (append assignments exps))))))
480 665c255d 2023-08-04 jrmu (define (compound-procedure? p)
481 665c255d 2023-08-04 jrmu (tagged-list? p 'procedure))
482 665c255d 2023-08-04 jrmu (define (procedure-parameters p) (cadr p))
483 665c255d 2023-08-04 jrmu (define (procedure-body p) (caddr p))
484 665c255d 2023-08-04 jrmu (define (procedure-environment p) (cadddr p))
485 665c255d 2023-08-04 jrmu
486 665c255d 2023-08-04 jrmu ;; environment
487 665c255d 2023-08-04 jrmu (define (enclosing-environment env) (cdr env))
488 665c255d 2023-08-04 jrmu (define (first-frame env) (car env))
489 665c255d 2023-08-04 jrmu (define the-empty-environment '())
490 665c255d 2023-08-04 jrmu (define (make-frame variables values)
491 665c255d 2023-08-04 jrmu (cons variables values))
492 665c255d 2023-08-04 jrmu (define (frame-variables frame) (car frame))
493 665c255d 2023-08-04 jrmu (define (frame-values frame) (cdr frame))
494 665c255d 2023-08-04 jrmu (define (add-binding-to-frame! var val frame)
495 665c255d 2023-08-04 jrmu (set-car! frame (cons var (car frame)))
496 665c255d 2023-08-04 jrmu (set-cdr! frame (cons val (cdr frame))))
497 665c255d 2023-08-04 jrmu (define (extend-environment vars vals base-env)
498 665c255d 2023-08-04 jrmu (if (= (length vars) (length vals))
499 665c255d 2023-08-04 jrmu (cons (make-frame vars vals) base-env)
500 665c255d 2023-08-04 jrmu (if (< (length vars) (length vals))
501 665c255d 2023-08-04 jrmu (error "Too many arguments supplied" vars vals)
502 665c255d 2023-08-04 jrmu (error "Too few arguments supplied" vars vals))))
503 665c255d 2023-08-04 jrmu (define (lookup-variable-value var env)
504 665c255d 2023-08-04 jrmu (define (env-loop env)
505 665c255d 2023-08-04 jrmu (define (scan vars vals)
506 665c255d 2023-08-04 jrmu (cond ((null? vars)
507 665c255d 2023-08-04 jrmu (env-loop (enclosing-environment env)))
508 665c255d 2023-08-04 jrmu ((eq? var (car vars))
509 665c255d 2023-08-04 jrmu ;; (let ((val (car vals)))
510 665c255d 2023-08-04 jrmu ;; (if (eq? val '*unassigned*)
511 665c255d 2023-08-04 jrmu ;; (error "Var not yet defined -- LOOKUP-VARIABLE-VALUE" var)
512 665c255d 2023-08-04 jrmu ;; val)))
513 665c255d 2023-08-04 jrmu (car vals))
514 665c255d 2023-08-04 jrmu (else (scan (cdr vars) (cdr vals)))))
515 665c255d 2023-08-04 jrmu (if (eq? env the-empty-environment)
516 665c255d 2023-08-04 jrmu (error "Unbound variable" var)
517 665c255d 2023-08-04 jrmu (let ((frame (first-frame env)))
518 665c255d 2023-08-04 jrmu (scan (frame-variables frame)
519 665c255d 2023-08-04 jrmu (frame-values frame)))))
520 665c255d 2023-08-04 jrmu (env-loop env))
521 665c255d 2023-08-04 jrmu (define (set-variable-value! var val env)
522 665c255d 2023-08-04 jrmu (define (env-loop env)
523 665c255d 2023-08-04 jrmu (define (scan vars vals)
524 665c255d 2023-08-04 jrmu (cond ((null? vars)
525 665c255d 2023-08-04 jrmu (env-loop (enclosing-environment env)))
526 665c255d 2023-08-04 jrmu ((eq? var (car vars))
527 665c255d 2023-08-04 jrmu (set-car! vals val))
528 665c255d 2023-08-04 jrmu (else (scan (cdr vars) (cdr vals)))))
529 665c255d 2023-08-04 jrmu (if (eq? env the-empty-environment)
530 665c255d 2023-08-04 jrmu (error "Unbound variable -- SET!" var)
531 665c255d 2023-08-04 jrmu (let ((frame (first-frame env)))
532 665c255d 2023-08-04 jrmu (scan (frame-variables frame)
533 665c255d 2023-08-04 jrmu (frame-values frame)))))
534 665c255d 2023-08-04 jrmu (env-loop env))
535 665c255d 2023-08-04 jrmu (define (define-variable! var val env)
536 665c255d 2023-08-04 jrmu (let ((frame (first-frame env)))
537 665c255d 2023-08-04 jrmu (define (scan vars vals)
538 665c255d 2023-08-04 jrmu (cond ((null? vars)
539 665c255d 2023-08-04 jrmu (add-binding-to-frame! var val frame))
540 665c255d 2023-08-04 jrmu ((eq? var (car vars))
541 665c255d 2023-08-04 jrmu (set-car! vals val))
542 665c255d 2023-08-04 jrmu (else (scan (cdr vars) (cdr vals)))))
543 665c255d 2023-08-04 jrmu (scan (frame-variables frame)
544 665c255d 2023-08-04 jrmu (frame-values frame))))
545 665c255d 2023-08-04 jrmu
546 665c255d 2023-08-04 jrmu ;; (define (remove-binding-from-frame! var frame)
547 665c255d 2023-08-04 jrmu ;; (define (scan vars vals)
548 665c255d 2023-08-04 jrmu ;; (cond ((null? (cdr vars))
549 665c255d 2023-08-04 jrmu ;; (error "Binding not found -- REMOVE-BINDING-FROM-FRAME!" var))
550 665c255d 2023-08-04 jrmu ;; ((eq? var (cadr vars))
551 665c255d 2023-08-04 jrmu ;; (set-cdr! vars (cddr vars))
552 665c255d 2023-08-04 jrmu ;; (set-cdr! vals (cddr vals)))
553 665c255d 2023-08-04 jrmu ;; (else (scan (cdr vars) (cdr vals)))))
554 665c255d 2023-08-04 jrmu ;; (let ((vars (frame-variables frame))
555 665c255d 2023-08-04 jrmu ;; (vals (frame-values frame)))
556 665c255d 2023-08-04 jrmu ;; (if (eq? var (car vars))
557 665c255d 2023-08-04 jrmu ;; (begin (set-car! frame (cdr vars))
558 665c255d 2023-08-04 jrmu ;; (set-cdr! frame (cdr vals)))
559 665c255d 2023-08-04 jrmu ;; (scan vars vals))))
560 665c255d 2023-08-04 jrmu
561 665c255d 2023-08-04 jrmu ;; primitives
562 665c255d 2023-08-04 jrmu (define (primitive-procedure? proc)
563 665c255d 2023-08-04 jrmu (tagged-list? proc 'primitive))
564 665c255d 2023-08-04 jrmu (define (primitive-implementation proc) (cadr proc))
565 665c255d 2023-08-04 jrmu (define primitive-procedures
566 665c255d 2023-08-04 jrmu (list (list 'car car)
567 665c255d 2023-08-04 jrmu (list 'cdr cdr)
568 665c255d 2023-08-04 jrmu (list 'caar caar)
569 665c255d 2023-08-04 jrmu (list 'cadr cadr)
570 665c255d 2023-08-04 jrmu (list 'cddr cddr)
571 665c255d 2023-08-04 jrmu (list 'caddr caddr)
572 665c255d 2023-08-04 jrmu (list 'cdddr cdddr)
573 665c255d 2023-08-04 jrmu (list 'cons cons)
574 665c255d 2023-08-04 jrmu (list 'list list)
575 665c255d 2023-08-04 jrmu (list 'null? null?)
576 665c255d 2023-08-04 jrmu (list 'pair? pair?)
577 665c255d 2023-08-04 jrmu (list '* *)
578 665c255d 2023-08-04 jrmu (list '/ /)
579 665c255d 2023-08-04 jrmu (list '+ +)
580 665c255d 2023-08-04 jrmu (list '- -)
581 665c255d 2023-08-04 jrmu (list '= =)
582 665c255d 2023-08-04 jrmu (list '< <)
583 665c255d 2023-08-04 jrmu (list '> >)
584 665c255d 2023-08-04 jrmu (list '<= <=)
585 665c255d 2023-08-04 jrmu (list '>= >=)
586 665c255d 2023-08-04 jrmu (list 'abs abs)
587 665c255d 2023-08-04 jrmu (list 'remainder remainder)
588 665c255d 2023-08-04 jrmu (list 'eq? eq?)
589 665c255d 2023-08-04 jrmu (list 'equal? equal?)
590 665c255d 2023-08-04 jrmu (list 'member member)
591 665c255d 2023-08-04 jrmu (list 'memq memq)
592 665c255d 2023-08-04 jrmu (list 'display display)
593 665c255d 2023-08-04 jrmu (list 'error error)))
594 665c255d 2023-08-04 jrmu (define (primitive-procedure-names)
595 665c255d 2023-08-04 jrmu (map car
596 665c255d 2023-08-04 jrmu primitive-procedures))
597 665c255d 2023-08-04 jrmu (define (primitive-procedure-objects)
598 665c255d 2023-08-04 jrmu (map (lambda (proc) (list 'primitive (cadr proc)))
599 665c255d 2023-08-04 jrmu primitive-procedures))
600 665c255d 2023-08-04 jrmu (define (apply-primitive-procedure proc args)
601 665c255d 2023-08-04 jrmu (apply
602 665c255d 2023-08-04 jrmu (primitive-implementation proc) args))
603 665c255d 2023-08-04 jrmu
604 665c255d 2023-08-04 jrmu ;; execute application
605 665c255d 2023-08-04 jrmu
606 665c255d 2023-08-04 jrmu (define (execute-application proc args succeed fail)
607 665c255d 2023-08-04 jrmu (cond ((primitive-procedure? proc)
608 665c255d 2023-08-04 jrmu (succeed (apply-primitive-procedure proc args)
609 665c255d 2023-08-04 jrmu fail))
610 665c255d 2023-08-04 jrmu ((compound-procedure? proc)
611 665c255d 2023-08-04 jrmu ((procedure-body proc)
612 665c255d 2023-08-04 jrmu (extend-environment (procedure-parameters proc)
613 665c255d 2023-08-04 jrmu args
614 665c255d 2023-08-04 jrmu (procedure-environment proc))
615 665c255d 2023-08-04 jrmu succeed
616 665c255d 2023-08-04 jrmu fail))
617 665c255d 2023-08-04 jrmu (else
618 665c255d 2023-08-04 jrmu (error
619 665c255d 2023-08-04 jrmu "Unknown procedure type -- EXECUTE-APPLICATION"
620 665c255d 2023-08-04 jrmu proc))))
621 665c255d 2023-08-04 jrmu
622 665c255d 2023-08-04 jrmu
623 665c255d 2023-08-04 jrmu ;; driver-loop
624 665c255d 2023-08-04 jrmu (define (prompt-for-input string)
625 665c255d 2023-08-04 jrmu (newline) (newline) (display string) (newline))
626 665c255d 2023-08-04 jrmu (define (announce-output string)
627 665c255d 2023-08-04 jrmu (newline) (display string) (newline))
628 665c255d 2023-08-04 jrmu (define (user-print object)
629 665c255d 2023-08-04 jrmu (if (compound-procedure? object)
630 665c255d 2023-08-04 jrmu (display (list 'compound-procedure
631 665c255d 2023-08-04 jrmu (procedure-parameters object)
632 665c255d 2023-08-04 jrmu (procedure-body object)
633 665c255d 2023-08-04 jrmu '<procedure-env>))
634 665c255d 2023-08-04 jrmu (display object)))
635 665c255d 2023-08-04 jrmu (define (setup-environment)
636 665c255d 2023-08-04 jrmu (let ((initial-env
637 665c255d 2023-08-04 jrmu (extend-environment (primitive-procedure-names)
638 665c255d 2023-08-04 jrmu (primitive-procedure-objects)
639 665c255d 2023-08-04 jrmu the-empty-environment)))
640 665c255d 2023-08-04 jrmu (define-variable! 'true true initial-env)
641 665c255d 2023-08-04 jrmu (define-variable! 'false false initial-env)
642 665c255d 2023-08-04 jrmu initial-env))
643 665c255d 2023-08-04 jrmu (define the-global-environment (setup-environment))
644 665c255d 2023-08-04 jrmu
645 665c255d 2023-08-04 jrmu (define input-prompt ";;; Amb-Eval input:")
646 665c255d 2023-08-04 jrmu (define output-prompt ";;; Amb-Eval value:")
647 665c255d 2023-08-04 jrmu (define (driver-loop)
648 665c255d 2023-08-04 jrmu (define (internal-loop try-again)
649 665c255d 2023-08-04 jrmu (prompt-for-input input-prompt)
650 665c255d 2023-08-04 jrmu (let ((input (read)))
651 665c255d 2023-08-04 jrmu (if (eq? input 'try-again)
652 665c255d 2023-08-04 jrmu (try-again)
653 665c255d 2023-08-04 jrmu (begin
654 665c255d 2023-08-04 jrmu (newline)
655 665c255d 2023-08-04 jrmu (display ";;; Starting a new problem ")
656 665c255d 2023-08-04 jrmu (ambeval input
657 665c255d 2023-08-04 jrmu the-global-environment
658 665c255d 2023-08-04 jrmu ;; ambeval success
659 665c255d 2023-08-04 jrmu (lambda (val next-alternative)
660 665c255d 2023-08-04 jrmu (announce-output output-prompt)
661 665c255d 2023-08-04 jrmu (user-print val)
662 665c255d 2023-08-04 jrmu (internal-loop next-alternative))
663 665c255d 2023-08-04 jrmu ;; ambeval failure
664 665c255d 2023-08-04 jrmu (lambda ()
665 665c255d 2023-08-04 jrmu (announce-output
666 665c255d 2023-08-04 jrmu ";;; There are no more values of")
667 665c255d 2023-08-04 jrmu (user-print input)
668 665c255d 2023-08-04 jrmu (driver-loop)))))))
669 665c255d 2023-08-04 jrmu (internal-loop
670 665c255d 2023-08-04 jrmu (lambda ()
671 665c255d 2023-08-04 jrmu (newline)
672 665c255d 2023-08-04 jrmu (display ";;; There is no current problem")
673 665c255d 2023-08-04 jrmu (driver-loop))))
674 665c255d 2023-08-04 jrmu
675 665c255d 2023-08-04 jrmu
676 665c255d 2023-08-04 jrmu ;; auxiliary
677 665c255d 2023-08-04 jrmu (define (test-case actual expected)
678 665c255d 2023-08-04 jrmu (newline)
679 665c255d 2023-08-04 jrmu (display "Actual: ")
680 665c255d 2023-08-04 jrmu (display actual)
681 665c255d 2023-08-04 jrmu (newline)
682 665c255d 2023-08-04 jrmu (display "Expected: ")
683 665c255d 2023-08-04 jrmu (display expected)
684 665c255d 2023-08-04 jrmu (newline))
685 665c255d 2023-08-04 jrmu (define try-again
686 665c255d 2023-08-04 jrmu (lambda ()
687 665c255d 2023-08-04 jrmu "No current problem"))
688 665c255d 2023-08-04 jrmu (define (geval exp) ;; eval globally
689 665c255d 2023-08-04 jrmu (if (eq? exp 'try-again)
690 665c255d 2023-08-04 jrmu (try-again)
691 665c255d 2023-08-04 jrmu (ambeval exp
692 665c255d 2023-08-04 jrmu the-global-environment
693 665c255d 2023-08-04 jrmu (lambda (val next-alternative)
694 665c255d 2023-08-04 jrmu (set! try-again next-alternative)
695 665c255d 2023-08-04 jrmu val)
696 665c255d 2023-08-04 jrmu (lambda ()
697 665c255d 2023-08-04 jrmu (set! try-again
698 665c255d 2023-08-04 jrmu (lambda ()
699 665c255d 2023-08-04 jrmu "No current problem"))
700 665c255d 2023-08-04 jrmu "No alternatives"))))
701 665c255d 2023-08-04 jrmu (define (test-eval exp expected)
702 665c255d 2023-08-04 jrmu (test-case (geval exp) expected))
703 665c255d 2023-08-04 jrmu (define (print-eval exp)
704 665c255d 2023-08-04 jrmu (user-print (geval exp)))
705 665c255d 2023-08-04 jrmu
706 665c255d 2023-08-04 jrmu ;; test-suite
707 665c255d 2023-08-04 jrmu
708 665c255d 2023-08-04 jrmu ;; procedure definitions
709 665c255d 2023-08-04 jrmu
710 665c255d 2023-08-04 jrmu (geval
711 665c255d 2023-08-04 jrmu '(define (append x y)
712 665c255d 2023-08-04 jrmu (if (null? x)
713 665c255d 2023-08-04 jrmu y
714 665c255d 2023-08-04 jrmu (cons (car x) (append (cdr x) y)))))
715 665c255d 2023-08-04 jrmu (geval
716 665c255d 2023-08-04 jrmu '(define (list-ref items n)
717 665c255d 2023-08-04 jrmu (if (= n 0)
718 665c255d 2023-08-04 jrmu (car items)
719 665c255d 2023-08-04 jrmu (list-ref (cdr items) (- n 1)))))
720 665c255d 2023-08-04 jrmu (geval
721 665c255d 2023-08-04 jrmu '(define (fold-left f init seq)
722 665c255d 2023-08-04 jrmu (if (null? seq)
723 665c255d 2023-08-04 jrmu init
724 665c255d 2023-08-04 jrmu (fold-left f
725 665c255d 2023-08-04 jrmu (f init (car seq))
726 665c255d 2023-08-04 jrmu (cdr seq)))))
727 665c255d 2023-08-04 jrmu (geval
728 665c255d 2023-08-04 jrmu '(define (enumerate-interval low high)
729 665c255d 2023-08-04 jrmu (if (> low high)
730 665c255d 2023-08-04 jrmu '()
731 665c255d 2023-08-04 jrmu (cons low (enumerate-interval (+ low 1) high)))))
732 665c255d 2023-08-04 jrmu (geval
733 665c255d 2023-08-04 jrmu '(define (assoc key records)
734 665c255d 2023-08-04 jrmu (cond ((null? records) false)
735 665c255d 2023-08-04 jrmu ((equal? key (caar records)) (car records))
736 665c255d 2023-08-04 jrmu (else (assoc key (cdr records))))))
737 665c255d 2023-08-04 jrmu
738 665c255d 2023-08-04 jrmu (geval
739 665c255d 2023-08-04 jrmu '(define (map proc list)
740 665c255d 2023-08-04 jrmu (if (null? list)
741 665c255d 2023-08-04 jrmu '()
742 665c255d 2023-08-04 jrmu (cons (proc (car list))
743 665c255d 2023-08-04 jrmu (map proc (cdr list))))))
744 665c255d 2023-08-04 jrmu (geval
745 665c255d 2023-08-04 jrmu '(define (map-2 proc l1 l2)
746 665c255d 2023-08-04 jrmu (if (null? l1)
747 665c255d 2023-08-04 jrmu '()
748 665c255d 2023-08-04 jrmu (cons (proc (car l1) (car l2))
749 665c255d 2023-08-04 jrmu (map-2 proc (cdr l1) (cdr l2))))))
750 665c255d 2023-08-04 jrmu
751 665c255d 2023-08-04 jrmu (geval
752 665c255d 2023-08-04 jrmu '(define (accumulate op initial sequence)
753 665c255d 2023-08-04 jrmu (if (null? sequence)
754 665c255d 2023-08-04 jrmu initial
755 665c255d 2023-08-04 jrmu (op (car sequence)
756 665c255d 2023-08-04 jrmu (accumulate op initial (cdr sequence))))))
757 665c255d 2023-08-04 jrmu
758 665c255d 2023-08-04 jrmu ;; ;; ;; all special forms
759 665c255d 2023-08-04 jrmu ;; (test-eval '(begin 5 6) 6)
760 665c255d 2023-08-04 jrmu ;; (test-eval '10 10)
761 665c255d 2023-08-04 jrmu ;; (geval '(define x 3))
762 665c255d 2023-08-04 jrmu ;; (test-eval 'x 3)
763 665c255d 2023-08-04 jrmu ;; (test-eval '(set! x -25) 'ok)
764 665c255d 2023-08-04 jrmu ;; (test-eval 'x -25)
765 665c255d 2023-08-04 jrmu ;; (geval '(define z (lambda (x y) (+ x (* x y)))))
766 665c255d 2023-08-04 jrmu ;; (test-eval '(z 3 4) 15)
767 665c255d 2023-08-04 jrmu ;; (test-eval '(cond ((= x -2) 'x=-2)
768 665c255d 2023-08-04 jrmu ;; ((= x -25) 'x=-25)
769 665c255d 2023-08-04 jrmu ;; (else 'failed))
770 665c255d 2023-08-04 jrmu ;; 'x=-25)
771 665c255d 2023-08-04 jrmu ;; (test-eval '(if true false true) false)
772 665c255d 2023-08-04 jrmu
773 665c255d 2023-08-04 jrmu ;; (test-eval
774 665c255d 2023-08-04 jrmu ;; '(let ((x 4) (y 7))
775 665c255d 2023-08-04 jrmu ;; (+ x y (* x y)))
776 665c255d 2023-08-04 jrmu ;; (+ 4 7 (* 4 7)))
777 665c255d 2023-08-04 jrmu
778 665c255d 2023-08-04 jrmu
779 665c255d 2023-08-04 jrmu ;; ;; and/or
780 665c255d 2023-08-04 jrmu ;; (geval '(define x (+ 3 8)))
781 665c255d 2023-08-04 jrmu ;; (test-eval '(and 0 true x) 11)
782 665c255d 2023-08-04 jrmu ;; (test-eval '(and 0 true x false) false)
783 665c255d 2023-08-04 jrmu ;; (test-eval '(and 0 true x (set! x -2) false) false)
784 665c255d 2023-08-04 jrmu ;; (test-eval 'x -2)
785 665c255d 2023-08-04 jrmu ;; (test-eval '(and 0 true x false (set! x -5)) false)
786 665c255d 2023-08-04 jrmu ;; (test-eval 'x -2)
787 665c255d 2023-08-04 jrmu ;; (test-eval '(or false (set! x 25)) 'ok)
788 665c255d 2023-08-04 jrmu ;; (test-eval 'x 25)
789 665c255d 2023-08-04 jrmu ;; (test-eval '(or (set! x 2) (set! x 4)) 'ok)
790 665c255d 2023-08-04 jrmu ;; (test-eval 'x 2)
791 665c255d 2023-08-04 jrmu ;; (test-eval '(or false (set! x 25) true false) 'ok)
792 665c255d 2023-08-04 jrmu ;; (test-eval 'x 25)
793 665c255d 2023-08-04 jrmu ;; (test-eval '(or ((lambda (x) x) 5)) 5)
794 665c255d 2023-08-04 jrmu ;; (test-eval '(or (begin (set! x (+ x 1)) x)) 26) ;; this fails because or->if repeats the same clause twice
795 665c255d 2023-08-04 jrmu ;; (newline)
796 665c255d 2023-08-04 jrmu ;; (display "Failure expected")
797 665c255d 2023-08-04 jrmu ;; (newline)
798 665c255d 2023-08-04 jrmu
799 665c255d 2023-08-04 jrmu ;; ;; cond
800 665c255d 2023-08-04 jrmu
801 665c255d 2023-08-04 jrmu ;; (test-eval
802 665c255d 2023-08-04 jrmu ;; '(cond ((assoc 'b '((a 1) (b 2))) => cadr)
803 665c255d 2023-08-04 jrmu ;; (else false))
804 665c255d 2023-08-04 jrmu ;; 2)
805 665c255d 2023-08-04 jrmu
806 665c255d 2023-08-04 jrmu ;; (test-eval
807 665c255d 2023-08-04 jrmu ;; '(cond ((= 3 4) 'not-true)
808 665c255d 2023-08-04 jrmu ;; ((= (* 2 4) 3) 'also-false)
809 665c255d 2023-08-04 jrmu ;; ((map (lambda (x)
810 665c255d 2023-08-04 jrmu ;; (* x (+ x 1)))
811 665c255d 2023-08-04 jrmu ;; '(2 4 1 9))
812 665c255d 2023-08-04 jrmu ;; =>
813 665c255d 2023-08-04 jrmu ;; (lambda (x)
814 665c255d 2023-08-04 jrmu ;; (accumulate + 0 x)))
815 665c255d 2023-08-04 jrmu ;; (else 'never-reach))
816 665c255d 2023-08-04 jrmu ;; 118)
817 665c255d 2023-08-04 jrmu ;; ;; '(6 20 2 90)
818 665c255d 2023-08-04 jrmu
819 665c255d 2023-08-04 jrmu
820 665c255d 2023-08-04 jrmu ;; ;; procedure definition and application
821 665c255d 2023-08-04 jrmu ;; (geval
822 665c255d 2023-08-04 jrmu ;; '(define (factorial n)
823 665c255d 2023-08-04 jrmu ;; (if (= n 0)
824 665c255d 2023-08-04 jrmu ;; 1
825 665c255d 2023-08-04 jrmu ;; (* n (factorial (- n 1))))))
826 665c255d 2023-08-04 jrmu ;; (test-eval '(factorial 5) 120)
827 665c255d 2023-08-04 jrmu
828 665c255d 2023-08-04 jrmu ;; ;; map
829 665c255d 2023-08-04 jrmu
830 665c255d 2023-08-04 jrmu ;; (test-eval
831 665c255d 2023-08-04 jrmu ;; '(map (lambda (x)
832 665c255d 2023-08-04 jrmu ;; (* x (+ x 1)))
833 665c255d 2023-08-04 jrmu ;; '(2 1 4 2 8 3))
834 665c255d 2023-08-04 jrmu ;; '(6 2 20 6 72 12))
835 665c255d 2023-08-04 jrmu ;; ;; accumulate
836 665c255d 2023-08-04 jrmu
837 665c255d 2023-08-04 jrmu ;; (test-eval
838 665c255d 2023-08-04 jrmu ;; '(accumulate + 0 '(1 2 3 4 5))
839 665c255d 2023-08-04 jrmu ;; 15)
840 665c255d 2023-08-04 jrmu
841 665c255d 2023-08-04 jrmu ;; ;; make-let
842 665c255d 2023-08-04 jrmu ;; (test-eval
843 665c255d 2023-08-04 jrmu ;; (make-let '(x y) '(3 5) '((+ x y)))
844 665c255d 2023-08-04 jrmu ;; 8)
845 665c255d 2023-08-04 jrmu ;; (test-eval
846 665c255d 2023-08-04 jrmu ;; '(let ()
847 665c255d 2023-08-04 jrmu ;; 5)
848 665c255d 2023-08-04 jrmu ;; 5)
849 665c255d 2023-08-04 jrmu ;; (test-eval
850 665c255d 2023-08-04 jrmu ;; '(let ((x 3))
851 665c255d 2023-08-04 jrmu ;; x)
852 665c255d 2023-08-04 jrmu ;; 3)
853 665c255d 2023-08-04 jrmu ;; (test-eval
854 665c255d 2023-08-04 jrmu ;; '(let ((x 3)
855 665c255d 2023-08-04 jrmu ;; (y 5))
856 665c255d 2023-08-04 jrmu ;; (+ x y))
857 665c255d 2023-08-04 jrmu ;; 8)
858 665c255d 2023-08-04 jrmu ;; (test-eval
859 665c255d 2023-08-04 jrmu ;; '(let ((x 3)
860 665c255d 2023-08-04 jrmu ;; (y 2))
861 665c255d 2023-08-04 jrmu ;; (+ (let ((x (+ y 2))
862 665c255d 2023-08-04 jrmu ;; (y x))
863 665c255d 2023-08-04 jrmu ;; (* x y))
864 665c255d 2023-08-04 jrmu ;; x y))
865 665c255d 2023-08-04 jrmu ;; (+ (* 4 3) 3 2))
866 665c255d 2023-08-04 jrmu ;; (test-eval
867 665c255d 2023-08-04 jrmu ;; '(let ((x 6)
868 665c255d 2023-08-04 jrmu ;; (y (let ((x 2))
869 665c255d 2023-08-04 jrmu ;; (+ x 3)))
870 665c255d 2023-08-04 jrmu ;; (z (let ((a (* 3 2)))
871 665c255d 2023-08-04 jrmu ;; (+ a 3))))
872 665c255d 2023-08-04 jrmu ;; (+ x y z))
873 665c255d 2023-08-04 jrmu ;; (+ 6 5 9))
874 665c255d 2023-08-04 jrmu
875 665c255d 2023-08-04 jrmu
876 665c255d 2023-08-04 jrmu ;; ;; let*
877 665c255d 2023-08-04 jrmu
878 665c255d 2023-08-04 jrmu ;; (test-eval
879 665c255d 2023-08-04 jrmu ;; '(let* ((x 3)
880 665c255d 2023-08-04 jrmu ;; (y (+ x 2))
881 665c255d 2023-08-04 jrmu ;; (z (+ x y 5)))
882 665c255d 2023-08-04 jrmu ;; (* x z))
883 665c255d 2023-08-04 jrmu ;; 39)
884 665c255d 2023-08-04 jrmu
885 665c255d 2023-08-04 jrmu ;; (test-eval
886 665c255d 2023-08-04 jrmu ;; '(let* ()
887 665c255d 2023-08-04 jrmu ;; 5)
888 665c255d 2023-08-04 jrmu ;; 5)
889 665c255d 2023-08-04 jrmu ;; (test-eval
890 665c255d 2023-08-04 jrmu ;; '(let* ((x 3))
891 665c255d 2023-08-04 jrmu ;; (let* ((y 5))
892 665c255d 2023-08-04 jrmu ;; (+ x y)))
893 665c255d 2023-08-04 jrmu ;; 8)
894 665c255d 2023-08-04 jrmu
895 665c255d 2023-08-04 jrmu ;; (test-eval
896 665c255d 2023-08-04 jrmu ;; '(let* ((x 3)
897 665c255d 2023-08-04 jrmu ;; (y (+ x 1)))
898 665c255d 2023-08-04 jrmu ;; (+ (let* ((x (+ y 2))
899 665c255d 2023-08-04 jrmu ;; (y x))
900 665c255d 2023-08-04 jrmu ;; (* x y))
901 665c255d 2023-08-04 jrmu ;; x y))
902 665c255d 2023-08-04 jrmu ;; (+ (* 6 6) 3 4))
903 665c255d 2023-08-04 jrmu ;; (test-eval
904 665c255d 2023-08-04 jrmu ;; '(let* ((x 6)
905 665c255d 2023-08-04 jrmu ;; (y (let* ((x 2)
906 665c255d 2023-08-04 jrmu ;; (a (let* ((x (* 3 x)))
907 665c255d 2023-08-04 jrmu ;; (+ x 2))))
908 665c255d 2023-08-04 jrmu ;; (+ x a)))
909 665c255d 2023-08-04 jrmu ;; (z (+ x y)))
910 665c255d 2023-08-04 jrmu ;; (+ x y z))
911 665c255d 2023-08-04 jrmu ;; 32)
912 665c255d 2023-08-04 jrmu
913 665c255d 2023-08-04 jrmu ;; ;; named-let
914 665c255d 2023-08-04 jrmu
915 665c255d 2023-08-04 jrmu ;; (test-eval
916 665c255d 2023-08-04 jrmu ;; '(let eight ()
917 665c255d 2023-08-04 jrmu ;; 5
918 665c255d 2023-08-04 jrmu ;; 7
919 665c255d 2023-08-04 jrmu ;; 8)
920 665c255d 2023-08-04 jrmu ;; 8)
921 665c255d 2023-08-04 jrmu ;; (test-eval
922 665c255d 2023-08-04 jrmu ;; '(let loop ((count 0))
923 665c255d 2023-08-04 jrmu ;; (if (= 100 count)
924 665c255d 2023-08-04 jrmu ;; count
925 665c255d 2023-08-04 jrmu ;; (loop (+ count 1))))
926 665c255d 2023-08-04 jrmu ;; 100)
927 665c255d 2023-08-04 jrmu ;; (geval
928 665c255d 2023-08-04 jrmu ;; '(define (prime? x)
929 665c255d 2023-08-04 jrmu ;; (let prime-iter ((i 2))
930 665c255d 2023-08-04 jrmu ;; (cond ((> (* i i) x) true)
931 665c255d 2023-08-04 jrmu ;; ((= (remainder x i) 0) false)
932 665c255d 2023-08-04 jrmu ;; (else (prime-iter (+ i 1)))))))
933 665c255d 2023-08-04 jrmu ;; (test-eval
934 665c255d 2023-08-04 jrmu ;; '(let primes ((x 2)
935 665c255d 2023-08-04 jrmu ;; (n 20))
936 665c255d 2023-08-04 jrmu ;; (cond ((= n 0) '())
937 665c255d 2023-08-04 jrmu ;; ((prime? x)
938 665c255d 2023-08-04 jrmu ;; (cons x
939 665c255d 2023-08-04 jrmu ;; (primes (+ x 1) (- n 1))))
940 665c255d 2023-08-04 jrmu ;; (else (primes (+ x 1) n))))
941 665c255d 2023-08-04 jrmu ;; '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71))
942 665c255d 2023-08-04 jrmu
943 665c255d 2023-08-04 jrmu ;; (geval
944 665c255d 2023-08-04 jrmu ;; '(define (fib n)
945 665c255d 2023-08-04 jrmu ;; (let fib-iter ((a 1)
946 665c255d 2023-08-04 jrmu ;; (b 0)
947 665c255d 2023-08-04 jrmu ;; (count n))
948 665c255d 2023-08-04 jrmu ;; (if (= count 0)
949 665c255d 2023-08-04 jrmu ;; b
950 665c255d 2023-08-04 jrmu ;; (fib-iter (+ a b) a (- count 1))))))
951 665c255d 2023-08-04 jrmu ;; (test-eval '(fib 19) 4181)
952 665c255d 2023-08-04 jrmu
953 665c255d 2023-08-04 jrmu ;; ;; do-loop
954 665c255d 2023-08-04 jrmu ;; (test-eval
955 665c255d 2023-08-04 jrmu ;; '(let ((y 0))
956 665c255d 2023-08-04 jrmu ;; (do ((x 0 (+ x 1)))
957 665c255d 2023-08-04 jrmu ;; ((= x 5) y)
958 665c255d 2023-08-04 jrmu ;; (set! y (+ y 1))))
959 665c255d 2023-08-04 jrmu ;; 5)
960 665c255d 2023-08-04 jrmu ;; (test-eval
961 665c255d 2023-08-04 jrmu ;; '(do ()
962 665c255d 2023-08-04 jrmu ;; (true))
963 665c255d 2023-08-04 jrmu ;; true)
964 665c255d 2023-08-04 jrmu ;; (test-eval
965 665c255d 2023-08-04 jrmu ;; '(do ()
966 665c255d 2023-08-04 jrmu ;; (true 5))
967 665c255d 2023-08-04 jrmu ;; 5)
968 665c255d 2023-08-04 jrmu ;; (test-eval
969 665c255d 2023-08-04 jrmu ;; '(let ((y 0))
970 665c255d 2023-08-04 jrmu ;; (do ()
971 665c255d 2023-08-04 jrmu ;; ((= y 5) y)
972 665c255d 2023-08-04 jrmu ;; (set! y (+ y 1))))
973 665c255d 2023-08-04 jrmu ;; 5)
974 665c255d 2023-08-04 jrmu
975 665c255d 2023-08-04 jrmu ;; (test-eval
976 665c255d 2023-08-04 jrmu ;; '(do ((y '(1 2 3 4)))
977 665c255d 2023-08-04 jrmu ;; ((null? y))
978 665c255d 2023-08-04 jrmu ;; (set! y (cdr y)))
979 665c255d 2023-08-04 jrmu ;; true)
980 665c255d 2023-08-04 jrmu ;; (test-eval
981 665c255d 2023-08-04 jrmu ;; '(let ((y 0))
982 665c255d 2023-08-04 jrmu ;; (do ((x 0 (+ x 1)))
983 665c255d 2023-08-04 jrmu ;; ((= x 5) y)
984 665c255d 2023-08-04 jrmu ;; (set! y (+ y 1))))
985 665c255d 2023-08-04 jrmu ;; 5)
986 665c255d 2023-08-04 jrmu ;; (test-eval
987 665c255d 2023-08-04 jrmu ;; '(let ((x '(1 3 5 7 9)))
988 665c255d 2023-08-04 jrmu ;; (do ((x x (cdr x))
989 665c255d 2023-08-04 jrmu ;; (sum 0 (+ sum (car x))))
990 665c255d 2023-08-04 jrmu ;; ((null? x) sum)))
991 665c255d 2023-08-04 jrmu ;; 25)
992 665c255d 2023-08-04 jrmu ;; (test-eval
993 665c255d 2023-08-04 jrmu ;; '(let ((z '()))
994 665c255d 2023-08-04 jrmu ;; (do ((x '(1 2 3 4) (cdr x))
995 665c255d 2023-08-04 jrmu ;; (y '(1 2 3 4 5 6 7 8) (cddr y)))
996 665c255d 2023-08-04 jrmu ;; ((null? x) y x z)
997 665c255d 2023-08-04 jrmu ;; (set! z (cons (car x) z))))
998 665c255d 2023-08-04 jrmu ;; '(4 3 2 1))
999 665c255d 2023-08-04 jrmu
1000 665c255d 2023-08-04 jrmu
1001 665c255d 2023-08-04 jrmu
1002 665c255d 2023-08-04 jrmu ;; ;; make-unbound!
1003 665c255d 2023-08-04 jrmu ;; ;; broken now due to scan-out-defines
1004 665c255d 2023-08-04 jrmu
1005 665c255d 2023-08-04 jrmu ;; ;; (test-eval
1006 665c255d 2023-08-04 jrmu ;; ;; '(let ((x 3))
1007 665c255d 2023-08-04 jrmu ;; ;; (let ((x 5))
1008 665c255d 2023-08-04 jrmu ;; ;; (make-unbound! x)
1009 665c255d 2023-08-04 jrmu ;; ;; (* x x)))
1010 665c255d 2023-08-04 jrmu ;; ;; 9)
1011 665c255d 2023-08-04 jrmu
1012 665c255d 2023-08-04 jrmu ;; ;; (test-eval
1013 665c255d 2023-08-04 jrmu ;; ;; '(let ((x 3))
1014 665c255d 2023-08-04 jrmu ;; ;; (let ((x 5))
1015 665c255d 2023-08-04 jrmu ;; ;; (define y x)
1016 665c255d 2023-08-04 jrmu ;; ;; (make-unbound! x)
1017 665c255d 2023-08-04 jrmu ;; ;; (* y x)))
1018 665c255d 2023-08-04 jrmu ;; ;; 15)
1019 665c255d 2023-08-04 jrmu
1020 665c255d 2023-08-04 jrmu ;; ;; (test-eval
1021 665c255d 2023-08-04 jrmu ;; ;; '(let ((y -1) (x 3))
1022 665c255d 2023-08-04 jrmu ;; ;; (let ((y 0.5) (x 5))
1023 665c255d 2023-08-04 jrmu ;; ;; (define a x)
1024 665c255d 2023-08-04 jrmu ;; ;; (define b y)
1025 665c255d 2023-08-04 jrmu ;; ;; (make-unbound! x)
1026 665c255d 2023-08-04 jrmu ;; ;; (make-unbound! y)
1027 665c255d 2023-08-04 jrmu ;; ;; (* a b x y)))
1028 665c255d 2023-08-04 jrmu ;; ;; (* 5 3 -1 0.5))
1029 665c255d 2023-08-04 jrmu
1030 665c255d 2023-08-04 jrmu ;; ;; (test-eval
1031 665c255d 2023-08-04 jrmu ;; ;; '(let ((x 3) (y 4))
1032 665c255d 2023-08-04 jrmu ;; ;; (let ((x 5))
1033 665c255d 2023-08-04 jrmu ;; ;; (make-unbound! x)
1034 665c255d 2023-08-04 jrmu ;; ;; (+ x 4)))
1035 665c255d 2023-08-04 jrmu ;; ;; 7)
1036 665c255d 2023-08-04 jrmu
1037 665c255d 2023-08-04 jrmu ;; ;; (test-eval
1038 665c255d 2023-08-04 jrmu ;; ;; '(let ((a 1) (b 2) (c 3) (d 4))
1039 665c255d 2023-08-04 jrmu ;; ;; (make-unbound! b)
1040 665c255d 2023-08-04 jrmu ;; ;; (+ a c d))
1041 665c255d 2023-08-04 jrmu ;; ;; (+ 1 3 4))
1042 665c255d 2023-08-04 jrmu
1043 665c255d 2023-08-04 jrmu ;; ;; (test-eval
1044 665c255d 2023-08-04 jrmu ;; ;; '(let ((x 4) (y 5))
1045 665c255d 2023-08-04 jrmu ;; ;; (let ((a 1) (b 2) (c 3))
1046 665c255d 2023-08-04 jrmu ;; ;; (let ((x (+ a b)) (y (+ c a)))
1047 665c255d 2023-08-04 jrmu ;; ;; (make-unbound! x)
1048 665c255d 2023-08-04 jrmu ;; ;; (let ((a x) (b (+ x y)))
1049 665c255d 2023-08-04 jrmu ;; ;; (define z b)
1050 665c255d 2023-08-04 jrmu ;; ;; (make-unbound! b)
1051 665c255d 2023-08-04 jrmu ;; ;; (* (+ a z)
1052 665c255d 2023-08-04 jrmu ;; ;; (+ a b y))))))
1053 665c255d 2023-08-04 jrmu ;; ;; (* (+ 4 8)
1054 665c255d 2023-08-04 jrmu ;; ;; (+ 4 2 4)))
1055 665c255d 2023-08-04 jrmu
1056 665c255d 2023-08-04 jrmu ;; ;; x 3 -- y 4
1057 665c255d 2023-08-04 jrmu ;; ;; x 4 -- y 4
1058 665c255d 2023-08-04 jrmu ;; ;; a 4 -- b 4
1059 665c255d 2023-08-04 jrmu ;; ;; a 4 -- b 2
1060 665c255d 2023-08-04 jrmu
1061 665c255d 2023-08-04 jrmu ;; ;; scan-out-defines
1062 665c255d 2023-08-04 jrmu
1063 665c255d 2023-08-04 jrmu ;; (geval
1064 665c255d 2023-08-04 jrmu ;; '(define (f x)
1065 665c255d 2023-08-04 jrmu ;; (define (even? n)
1066 665c255d 2023-08-04 jrmu ;; (if (= n 0)
1067 665c255d 2023-08-04 jrmu ;; true
1068 665c255d 2023-08-04 jrmu ;; (odd? (- n 1))))
1069 665c255d 2023-08-04 jrmu ;; (define (odd? n)
1070 665c255d 2023-08-04 jrmu ;; (if (= n 0)
1071 665c255d 2023-08-04 jrmu ;; false
1072 665c255d 2023-08-04 jrmu ;; (even? (- n 1))))
1073 665c255d 2023-08-04 jrmu ;; (even? x)))
1074 665c255d 2023-08-04 jrmu ;; (test-eval '(f 5) false)
1075 665c255d 2023-08-04 jrmu ;; (test-eval '(f 10) true)
1076 665c255d 2023-08-04 jrmu
1077 665c255d 2023-08-04 jrmu ;; ;; (geval
1078 665c255d 2023-08-04 jrmu ;; ;; '(let ((x 5))
1079 665c255d 2023-08-04 jrmu ;; ;; (define y x)
1080 665c255d 2023-08-04 jrmu ;; ;; (define x 3)
1081 665c255d 2023-08-04 jrmu ;; ;; (+ x y)))
1082 665c255d 2023-08-04 jrmu ;; ;; signal an error because x is undefined if variables are scanned out
1083 665c255d 2023-08-04 jrmu
1084 665c255d 2023-08-04 jrmu ;; ;; letrec
1085 665c255d 2023-08-04 jrmu
1086 665c255d 2023-08-04 jrmu ;; (geval
1087 665c255d 2023-08-04 jrmu ;; '(define (f x)
1088 665c255d 2023-08-04 jrmu ;; (letrec ((even?
1089 665c255d 2023-08-04 jrmu ;; (lambda (n)
1090 665c255d 2023-08-04 jrmu ;; (if (= n 0)
1091 665c255d 2023-08-04 jrmu ;; true
1092 665c255d 2023-08-04 jrmu ;; (odd? (- n 1)))))
1093 665c255d 2023-08-04 jrmu ;; (odd?
1094 665c255d 2023-08-04 jrmu ;; (lambda (n)
1095 665c255d 2023-08-04 jrmu ;; (if (= n 0)
1096 665c255d 2023-08-04 jrmu ;; false
1097 665c255d 2023-08-04 jrmu ;; (even? (- n 1))))))
1098 665c255d 2023-08-04 jrmu ;; (even? x))))
1099 665c255d 2023-08-04 jrmu ;; (test-eval '(f 11) false)
1100 665c255d 2023-08-04 jrmu ;; (test-eval '(f 16) true)
1101 665c255d 2023-08-04 jrmu
1102 665c255d 2023-08-04 jrmu ;; (test-eval
1103 665c255d 2023-08-04 jrmu ;; '(letrec ((fact
1104 665c255d 2023-08-04 jrmu ;; (lambda (n)
1105 665c255d 2023-08-04 jrmu ;; (if (= n 1)
1106 665c255d 2023-08-04 jrmu ;; 1
1107 665c255d 2023-08-04 jrmu ;; (* n (fact (- n 1)))))))
1108 665c255d 2023-08-04 jrmu ;; (fact 10))
1109 665c255d 2023-08-04 jrmu ;; 3628800)
1110 665c255d 2023-08-04 jrmu
1111 665c255d 2023-08-04 jrmu ;; amb
1112 665c255d 2023-08-04 jrmu (geval '(define (require p) (if (not p) (amb))))
1113 665c255d 2023-08-04 jrmu
1114 665c255d 2023-08-04 jrmu ;; (test-eval '(amb 1 2 3) 1)
1115 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again 2)
1116 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again 3)
1117 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again "No alternatives")
1118 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again "No current problem")
1119 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again "No current problem")
1120 665c255d 2023-08-04 jrmu ;; (test-eval '(amb (amb 1 2) (amb 'a 'b))
1121 665c255d 2023-08-04 jrmu ;; 1)
1122 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again 2)
1123 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again 'a)
1124 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again 'b)
1125 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again "No alternatives")
1126 665c255d 2023-08-04 jrmu ;; (test-eval '(require false) "No alternatives")
1127 665c255d 2023-08-04 jrmu ;; (test-eval '(require true) false)
1128 665c255d 2023-08-04 jrmu
1129 665c255d 2023-08-04 jrmu
1130 665c255d 2023-08-04 jrmu (geval
1131 665c255d 2023-08-04 jrmu '(define (an-integer-between low high)
1132 665c255d 2023-08-04 jrmu (require (<= low high))
1133 665c255d 2023-08-04 jrmu (amb low (an-integer-between (+ low 1) high))))
1134 665c255d 2023-08-04 jrmu
1135 665c255d 2023-08-04 jrmu (geval
1136 665c255d 2023-08-04 jrmu '(define (a-pythagorean-triple-between low high)
1137 665c255d 2023-08-04 jrmu (let ((i (an-integer-between low high)))
1138 665c255d 2023-08-04 jrmu (let ((j (an-integer-between i high)))
1139 665c255d 2023-08-04 jrmu (let ((k (an-integer-between j high)))
1140 665c255d 2023-08-04 jrmu (require (= (+ (* i i) (* j j)) (* k k)))
1141 665c255d 2023-08-04 jrmu (list i j k))))))
1142 665c255d 2023-08-04 jrmu
1143 665c255d 2023-08-04 jrmu ;; (test-eval
1144 665c255d 2023-08-04 jrmu ;; '(a-pythagorean-triple-between 1 20)
1145 665c255d 2023-08-04 jrmu ;; '(3 4 5))
1146 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again '(5 12 13))
1147 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again '(6 8 10))
1148 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again '(8 15 17))
1149 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again '(9 12 15))
1150 665c255d 2023-08-04 jrmu
1151 665c255d 2023-08-04 jrmu (geval
1152 665c255d 2023-08-04 jrmu '(define (an-integer-starting-from low)
1153 665c255d 2023-08-04 jrmu (amb low (an-integer-starting-from (+ low 1)))))
1154 665c255d 2023-08-04 jrmu
1155 665c255d 2023-08-04 jrmu (geval
1156 665c255d 2023-08-04 jrmu '(define (pythagorean-triples-starting-from low)
1157 665c255d 2023-08-04 jrmu (let* ((k (an-integer-starting-from low))
1158 665c255d 2023-08-04 jrmu (i (an-integer-between low k))
1159 665c255d 2023-08-04 jrmu (j (an-integer-between i k)))
1160 665c255d 2023-08-04 jrmu (require (= (+ (* i i) (* j j)) (* k k)))
1161 665c255d 2023-08-04 jrmu (list i j k))))
1162 665c255d 2023-08-04 jrmu
1163 665c255d 2023-08-04 jrmu ;; (test-eval '(pythagorean-triples-starting-from 1)
1164 665c255d 2023-08-04 jrmu ;; '(3 4 5))
1165 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again '(6 8 10))
1166 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again '(5 12 13))
1167 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again '(9 12 15))
1168 665c255d 2023-08-04 jrmu ;; (test-eval 'try-again '(8 15 17))
1169 665c255d 2023-08-04 jrmu
1170 665c255d 2023-08-04 jrmu (geval
1171 665c255d 2023-08-04 jrmu '(define (next-triplet trp)
1172 665c255d 2023-08-04 jrmu (let ((i (car trp))
1173 665c255d 2023-08-04 jrmu (j (cadr trp))
1174 665c255d 2023-08-04 jrmu (k (caddr trp)))
1175 665c255d 2023-08-04 jrmu (cond ((= i j k) (list 1 1 (+ k 1)))
1176 665c255d 2023-08-04 jrmu ((= j k) (list (+ i 1) (+ i 1) k))
1177 665c255d 2023-08-04 jrmu (else (list i (+ j 1) k))))))
1178 665c255d 2023-08-04 jrmu (geval
1179 665c255d 2023-08-04 jrmu '(define (triplet-starting-from trp)
1180 665c255d 2023-08-04 jrmu (amb trp (triplet-starting-from (next-triplet trp)))))
1181 665c255d 2023-08-04 jrmu (geval
1182 665c255d 2023-08-04 jrmu '(define (pythagorean-triples-starting-from low)
1183 665c255d 2023-08-04 jrmu (let* ((triplet (triplet-starting-from (list low low low)))
1184 665c255d 2023-08-04 jrmu (i (car triplet))
1185 665c255d 2023-08-04 jrmu (j (cadr triplet))
1186 665c255d 2023-08-04 jrmu (k (caddr triplet)))
1187 665c255d 2023-08-04 jrmu (require (= (+ (* i i) (* j j)) (* k k)))
1188 665c255d 2023-08-04 jrmu (list i j k))))
1189 665c255d 2023-08-04 jrmu (geval
1190 665c255d 2023-08-04 jrmu '(define (distinct? items)
1191 665c255d 2023-08-04 jrmu (cond ((null? items) true)
1192 665c255d 2023-08-04 jrmu ((null? (cdr items)) true)
1193 665c255d 2023-08-04 jrmu ((member (car items) (cdr items)) false)
1194 665c255d 2023-08-04 jrmu (else (distinct? (cdr items))))))
1195 665c255d 2023-08-04 jrmu
1196 665c255d 2023-08-04 jrmu ;; Exercise 4.51. Implement a new kind of assignment called permanent-set! that is not undone upon failure. For example, we can choose two distinct elements from a list and count the number of trials required to make a successful choice as follows:
1197 665c255d 2023-08-04 jrmu
1198 665c255d 2023-08-04 jrmu (geval
1199 665c255d 2023-08-04 jrmu '(define (an-element-of items)
1200 665c255d 2023-08-04 jrmu (require (not (null? items)))
1201 665c255d 2023-08-04 jrmu (amb (car items) (an-element-of (cdr items)))))
1202 665c255d 2023-08-04 jrmu
1203 665c255d 2023-08-04 jrmu (geval
1204 665c255d 2023-08-04 jrmu '(define count 0))
1205 665c255d 2023-08-04 jrmu (test-eval
1206 665c255d 2023-08-04 jrmu '(let ((x (an-element-of '(a b c)))
1207 665c255d 2023-08-04 jrmu (y (an-element-of '(a b c))))
1208 665c255d 2023-08-04 jrmu (permanent-set! count (+ count 1))
1209 665c255d 2023-08-04 jrmu (require (not (eq? x y)))
1210 665c255d 2023-08-04 jrmu (list x y count))
1211 665c255d 2023-08-04 jrmu '(a b 2))
1212 665c255d 2023-08-04 jrmu (test-eval 'try-again '(a c 3))
1213 665c255d 2023-08-04 jrmu
1214 665c255d 2023-08-04 jrmu ;; What values would have been displayed if we had used set! here rather than permanent-set! ?
1215 665c255d 2023-08-04 jrmu
1216 665c255d 2023-08-04 jrmu ;; if we had used set!, we would end up with (a b 1) and (a c 1). Count would always just be 1.
1217 665c255d 2023-08-04 jrmu