Blob


1 (define (eval exp env)
2 (cond ((self-evaluating? exp) exp)
3 ((variable? exp) (lookup-variable-value exp env))
4 ((quoted? exp) (text-of-quotation exp))
5 ((assignment? exp) (eval-assignment exp env))
6 ((definition? exp) (eval-definition exp env))
7 ((if? exp) (eval-if exp env))
8 ((and? exp) (eval-and exp env))
9 ((or? exp) (eval-or exp env))
10 ((lambda? exp)
11 (make-procedure (lambda-parameters exp)
12 (lambda-body exp)
13 env))
14 ((begin? exp)
15 (eval-sequence (begin-actions exp) env))
16 ((cond? exp) (eval (cond->if exp) env))
17 ((let? exp) (eval (let->combination exp) env))
18 ((let*? exp) (eval (let*->nested-lets exp) env))
19 ((named-let? exp) (eval (named-let->combination exp) env))
20 ((do? exp) (eval (do->combination exp) env))
21 ((application? exp)
22 (apply (eval (operator exp) env)
23 (list-of-values (operands exp) env)))
24 (else
25 (error "Unknown expression type -- EVAL" exp))))
26 (define (apply procedure arguments)
27 (cond ((primitive-procedure? procedure)
28 (apply-primitive-procedure procedure arguments))
29 ((compound-procedure? procedure)
30 (eval-sequence
31 (procedure-body procedure)
32 (extend-environment
33 (procedure-parameters procedure)
34 arguments
35 (procedure-environment procedure))))
36 (else
37 (error
38 "Unknown procedure type -- APPLY" procedure))))
40 (define (list-of-values exps env)
41 (if (no-operands? exps)
42 '()
43 (cons (eval (first-operand exps) env)
44 (list-of-values (rest-operands exps) env))))
46 (define (tagged-list? exp tag)
47 (if (pair? exp)
48 (eq? (car exp) tag)
49 false))
51 ;; self-evaluating/variable/quoted
52 (define (self-evaluating? exp)
53 (cond ((number? exp) true)
54 ((string? exp) true)
55 (else false)))
56 (define (variable? exp) (symbol? exp))
57 (define (quoted? exp)
58 (tagged-list? exp 'quote))
59 (define (text-of-quotation exp) (cadr exp))
61 ;; assignment/definition
62 (define (assignment? exp)
63 (tagged-list? exp 'set!))
64 (define (assignment-variable exp) (cadr exp))
65 (define (assignment-value exp) (caddr exp))
66 (define (definition? exp)
67 (tagged-list? exp 'define))
68 (define (definition-variable exp)
69 (if (symbol? (cadr exp))
70 (cadr exp)
71 (caadr exp)))
72 (define (definition-value exp)
73 (if (symbol? (cadr exp))
74 (caddr exp)
75 (make-lambda (cdadr exp) ; formal parameters
76 (cddr exp)))) ; body
77 (define (eval-assignment exp env)
78 (set-variable-value! (assignment-variable exp)
79 (eval (assignment-value exp) env)
80 env)
81 'ok)
82 (define (eval-definition exp env)
83 (define-variable! (definition-variable exp)
84 (eval (definition-value exp) env)
85 env)
86 'ok)
87 (define (make-definition var val)
88 `(define ,var ,val))
91 ;; if/and/or
92 (define (if? exp) (tagged-list? exp 'if))
93 (define (if-predicate exp) (cadr exp))
94 (define (if-consequent exp) (caddr exp))
95 (define (if-alternative exp)
96 (if (not (null? (cdddr exp)))
97 (cadddr exp)
98 'false))
99 (define (make-if predicate consequent alternative)
100 (list 'if predicate consequent alternative))
101 (define (eval-if exp env)
102 (if (true? (eval (if-predicate exp) env))
103 (eval (if-consequent exp) env)
104 (eval (if-alternative exp) env)))
106 (define (and? exp)
107 (tagged-list? exp 'and))
108 (define (and-clauses exp)
109 (cdr exp))
110 (define (or? exp)
111 (tagged-list? exp 'or))
112 (define (or-clauses exp)
113 (cdr exp))
114 (define (eval-and exp env)
115 (define (eval-clauses clauses)
116 (cond ((null? clauses) true)
117 ((null? (cdr clauses)) (eval (car clauses) env))
118 (else (and (eval (car clauses) env)
119 (eval-clauses (cdr clauses))))))
120 (eval-clauses (and-clauses exp)))
121 (define (eval-or exp env)
122 (define (eval-clauses clauses)
123 (if (null? clauses)
124 false
125 (or (eval (car clauses) env)
126 (eval-clauses (cdr clauses)))))
127 (eval-clauses (or-clauses exp)))
130 ;; lambda/let/let*
131 (define (lambda? exp) (tagged-list? exp 'lambda))
132 (define (lambda-parameters exp) (cadr exp))
133 (define (lambda-body exp) (cddr exp))
134 (define (make-lambda parameters body)
135 (cons 'lambda (cons parameters body)))
137 (define (make-let vars vals body)
138 (cons 'let
139 (cons (map (lambda (var val)
140 (list var val))
141 vars vals)
142 body)))
143 (define (let? exp)
144 (and (tagged-list? exp 'let)
145 (not (symbol? (cadr exp)))))
146 (define (let-vars exp)
147 (map car (cadr exp)))
148 (define (let-vals exp)
149 (map cadr (cadr exp)))
150 (define (let-body exp)
151 (cddr exp))
152 (define (let->combination exp)
153 (make-application (make-lambda (let-vars exp) (let-body exp))
154 (let-vals exp)))
155 (define (named-let? exp)
156 (and (tagged-list? exp 'let)
157 (symbol? (cadr exp))))
158 (define (named-let-name exp)
159 (cadr exp))
160 (define (named-let-vars exp)
161 (map car (caddr exp)))
162 (define (named-let-vals exp)
163 (map cadr (caddr exp)))
164 (define (named-let-body exp)
165 (cdddr exp))
166 (define (named-let->combination exp)
167 (sequence->exp
168 (list (make-definition (named-let-name exp)
169 (make-lambda (named-let-vars exp)
170 (named-let-body exp)))
171 (make-application (named-let-name exp)
172 (named-let-vals exp)))))
175 (define (make-application op args)
176 (cons op args))
178 (define (let*? exp)
179 (tagged-list? exp 'let*))
180 (define let*-vars let-vars)
181 (define let*-vals let-vals)
182 (define let*-body let-body)
183 (define (let*->nested-lets exp)
184 (define (expand-lets vars vals)
185 (if (null? (cdr vars))
186 (make-let (list (car vars))
187 (list (car vals))
188 (let*-body exp))
189 (make-let (list (car vars))
190 (list (car vals))
191 (list (expand-lets (cdr vars) (cdr vals))))))
192 (let ((vars (let*-vars exp))
193 (vals (let*-vals exp)))
194 (if (null? vars)
195 (sequence->exp (let*-body exp))
196 (expand-lets vars vals))))
199 ;; begin/sequence
200 (define (begin? exp) (tagged-list? exp 'begin))
201 (define (begin-actions exp) (cdr exp))
202 (define (last-exp? seq) (null? (cdr seq)))
203 (define (first-exp seq) (car seq))
204 (define (rest-exps seq) (cdr seq))
205 (define (sequence->exp seq)
206 (cond ((null? seq) seq)
207 ((last-exp? seq) (first-exp seq))
208 (else (make-begin seq))))
209 (define (make-begin seq) (cons 'begin seq))
210 (define (eval-sequence exps env)
211 (cond ((last-exp? exps) (eval (first-exp exps) env))
212 (else (eval (first-exp exps) env)
213 (eval-sequence (rest-exps exps) env))))
215 ;; application
216 (define (application? exp) (pair? exp))
217 (define (operator exp) (car exp))
218 (define (operands exp) (cdr exp))
219 (define (no-operands? ops) (null? ops))
220 (define (first-operand ops) (car ops))
221 (define (rest-operands ops) (cdr ops))
223 ;; cond
224 (define (cond? exp) (tagged-list? exp 'cond))
225 (define (cond-clauses exp) (cdr exp))
226 (define (cond-else-clause? clause)
227 (eq? (cond-predicate clause) 'else))
228 (define (cond-predicate clause) (car clause))
229 (define (cond-actions clause) (cdr clause))
230 (define (cond-extended-clause? clause)
231 (and (not (null? (cdr clause))) (eq? (cadr clause) '=>)))
232 (define (cond-extended-proc clause)
233 (caddr clause))
234 (define (cond->if exp)
235 (expand-clauses (cond-clauses exp)))
236 (define (expand-clauses clauses)
237 (if (null? clauses)
238 'false ; no else clause
239 (let ((first (car clauses))
240 (rest (cdr clauses)))
241 (if (cond-else-clause? first)
242 (if (null? rest)
243 (sequence->exp (cond-actions first))
244 (error "ELSE clause isn't last -- COND->IF"
245 clauses))
246 (if (cond-extended-clause? first)
247 (make-if (cond-predicate first)
248 (make-application
249 (cond-extended-proc first)
250 (list (cond-predicate first)))
251 (expand-clauses rest))
252 (make-if (cond-predicate first)
253 (sequence->exp (cond-actions first))
254 (expand-clauses rest)))))))
255 (define (true? x)
256 (not (eq? x false)))
257 (define (false? x)
258 (eq? x false))
260 ;; procedure
261 (define (make-procedure parameters body env)
262 (list 'procedure parameters body env))
263 (define (compound-procedure? p)
264 (tagged-list? p 'procedure))
265 (define (procedure-parameters p) (cadr p))
266 (define (procedure-body p) (caddr p))
267 (define (procedure-environment p) (cadddr p))
269 ;; environment
270 (define (enclosing-environment env) (cdr env))
271 (define (first-frame env) (car env))
272 (define the-empty-environment '())
273 (define (make-frame variables values)
274 (cons variables values))
275 (define (frame-variables frame) (car frame))
276 (define (frame-values frame) (cdr frame))
277 (define (add-binding-to-frame! var val frame)
278 (set-car! frame (cons var (car frame)))
279 (set-cdr! frame (cons val (cdr frame))))
280 (define (extend-environment vars vals base-env)
281 (if (= (length vars) (length vals))
282 (cons (make-frame vars vals) base-env)
283 (if (< (length vars) (length vals))
284 (error "Too many arguments supplied" vars vals)
285 (error "Too few arguments supplied" vars vals))))
286 (define (lookup-variable-value var env)
287 (define (env-loop env)
288 (define (scan vars vals)
289 (cond ((null? vars)
290 (env-loop (enclosing-environment env)))
291 ((eq? var (car vars))
292 (car vals))
293 (else (scan (cdr vars) (cdr vals)))))
294 (if (eq? env the-empty-environment)
295 (error "Unbound variable" var)
296 (let ((frame (first-frame env)))
297 (scan (frame-variables frame)
298 (frame-values frame)))))
299 (env-loop env))
300 (define (set-variable-value! var val env)
301 (define (env-loop env)
302 (define (scan vars vals)
303 (cond ((null? vars)
304 (env-loop (enclosing-environment env)))
305 ((eq? var (car vars))
306 (set-car! vals val))
307 (else (scan (cdr vars) (cdr vals)))))
308 (if (eq? env the-empty-environment)
309 (error "Unbound variable -- SET!" var)
310 (let ((frame (first-frame env)))
311 (scan (frame-variables frame)
312 (frame-values frame)))))
313 (env-loop env))
314 (define (define-variable! var val env)
315 (let ((frame (first-frame env)))
316 (define (scan vars vals)
317 (cond ((null? vars)
318 (add-binding-to-frame! var val frame))
319 ((eq? var (car vars))
320 (set-car! vals val))
321 (else (scan (cdr vars) (cdr vals)))))
322 (scan (frame-variables frame)
323 (frame-values frame))))
325 ;; primitives
326 (define (primitive-procedure? proc)
327 (tagged-list? proc 'primitive))
328 (define (primitive-implementation proc) (cadr proc))
329 (define primitive-procedures
330 (list (list 'car car)
331 (list 'cdr cdr)
332 (list 'caar caar)
333 (list 'cadr cadr)
334 (list 'cddr cddr)
335 (list 'cons cons)
336 (list 'null? null?)
337 (list '* *)
338 (list '/ /)
339 (list '+ +)
340 (list '- -)
341 (list '= =)
342 (list '< <)
343 (list '> >)
344 (list '<= <=)
345 (list '>= >=)
346 (list 'remainder remainder)
347 (list 'eq? eq?)
348 (list 'equal? equal?)
349 (list 'display display)))
350 (define (primitive-procedure-names)
351 (map car
352 primitive-procedures))
353 (define (primitive-procedure-objects)
354 (map (lambda (proc) (list 'primitive (cadr proc)))
355 primitive-procedures))
356 (define (apply-primitive-procedure proc args)
357 (apply-in-underlying-scheme
358 (primitive-implementation proc) args))
360 ;; driver-loop
361 (define input-prompt ";;; M-Eval input:")
362 (define output-prompt ";;; M-Eval value:")
363 (define (driver-loop)
364 (prompt-for-input input-prompt)
365 (let ((input (read)))
366 (let ((output (eval input the-global-environment)))
367 (announce-output output-prompt)
368 (user-print output)))
369 (driver-loop))
370 (define (prompt-for-input string)
371 (newline) (newline) (display string) (newline))
373 (define (announce-output string)
374 (newline) (display string) (newline))
375 (define (user-print object)
376 (if (compound-procedure? object)
377 (display (list 'compound-procedure
378 (procedure-parameters object)
379 (procedure-body object)
380 '<procedure-env>))
381 (display object)))
382 (define (setup-environment)
383 (let ((initial-env
384 (extend-environment (primitive-procedure-names)
385 (primitive-procedure-objects)
386 the-empty-environment)))
387 (define-variable! 'true true initial-env)
388 (define-variable! 'false false initial-env)
389 initial-env))
390 (define the-global-environment (setup-environment))
392 ;; auxiliary
393 (define (test-case actual expected)
394 (newline)
395 (display "Actual: ")
396 (display actual)
397 (newline)
398 (display "Expected: ")
399 (display expected)
400 (newline))
401 (define (geval exp) ;; eval globally
402 (eval exp the-global-environment))
403 (define (test-eval exp expected)
404 (test-case (geval exp) expected))
406 ;; Exercise 4.9. Many languages support a variety of iteration constructs, such as do, for, while, and until. In Scheme, iterative processes can be expressed in terms of ordinary procedure calls, so special iteration constructs provide no essential gain in computational power. On the other hand, such constructs are often convenient. Design some iteration constructs, give examples of their use, and show how to implement them as derived expressions.
408 ;; Implemented according to this spec:
409 ;; http://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Iteration.html
412 ;; (do ((vec (make-vector 5))
413 ;; (i 0 (+ i 1)))
414 ;; ((= i 5) vec)
415 ;; (vector-set! vec i i)) => #(0 1 2 3 4)
417 (define (do? exp)
418 (tagged-list? exp 'do))
419 (define (do-vars exp)
420 (map car (cadr exp)))
421 (define (do-inits exp)
422 (map cadr (cadr exp)))
423 (define (do-steps exp)
424 (map (lambda (var-init-step)
425 (if (null? (cddr var-init-step))
426 (car var-init-step)
427 (caddr var-init-step)))
428 (cadr exp)))
429 (define (do-test exp)
430 (caaddr exp))
431 (define (do-expressions exp)
432 (if (null? (cdaddr exp))
433 (caddr exp)
434 (cdaddr exp)))
435 (define (do-commands exp)
436 (cdddr exp))
437 (define (do->combination exp)
438 (sequence->exp
439 (list
440 (make-definition
441 'do-iter
442 (make-lambda
443 (do-vars exp)
444 (list
445 (make-if
446 (do-test exp)
447 (sequence->exp (do-expressions exp))
448 (sequence->exp
449 (append (do-commands exp)
450 (list (make-application
451 'do-iter
452 (do-steps exp)))))))))
453 (make-application 'do-iter (do-inits exp)))))
456 ;; (define do-name
457 ;; (lambda (x y)
458 ;; (if (null? x)
459 ;; (begin y x (car y)) ;; if there are no expressions, then (null? x) would be returned
460 ;; (begin (display 'command)
461 ;; (display 'another)
462 ;; (do-name (cdr x) (cddr y))) ;; if the step is omitted, it is simply x y
463 ;; (do-name '(1 2 3 4)
464 ;; '(1 2 3 4 5 6 7 8))
466 (test-eval
467 '(do ()
468 (true))
469 true)
470 (test-eval
471 '(do ()
472 (true 5))
473 5)
474 (test-eval
475 '(let ((y 0))
476 (do ()
477 ((= y 5) y)
478 (set! y (+ y 1))))
479 5)
481 (test-eval
482 '(do ((y '(1 2 3 4)))
483 ((null? y))
484 (set! y (cdr y)))
485 true)
486 (test-eval
487 '(let ((y 0))
488 (do ((x 0 (+ x 1)))
489 ((= x 5) y)
490 (set! y (+ y 1))))
491 5)
492 (test-eval
493 '(let ((x '(1 3 5 7 9)))
494 (do ((x x (cdr x))
495 (sum 0 (+ sum (car x))))
496 ((null? x) sum)))
497 25)
498 (test-eval
499 '(let ((z '()))
500 (do ((x '(1 2 3 4) (cdr x))
501 (y '(1 2 3 4 5 6 7 8) (cddr y)))
502 ((null? x) y x z)
503 (set! z (cons (car x) z))))
504 '(4 3 2 1))
508 ;; test-suite
510 ;; procedure definitions
512 (geval
513 '(define (assoc key records)
514 (cond ((null? records) false)
515 ((equal? key (caar records)) (car records))
516 (else (assoc key (cdr records))))))
518 (geval
519 '(define (map proc list)
520 (if (null? list)
521 '()
522 (cons (proc (car list))
523 (map proc (cdr list))))))
525 (geval
526 '(define (accumulate op initial sequence)
527 (if (null? sequence)
528 initial
529 (op (car sequence)
530 (accumulate op initial (cdr sequence))))))
532 ;; all special forms
533 (test-eval '(begin 5 6) 6)
534 (test-eval '10 10)
535 (geval '(define x 3))
536 (test-eval 'x 3)
537 (test-eval '(set! x -25) 'ok)
538 (test-eval 'x -25)
539 (geval '(define z (lambda (x y) (+ x (* x y)))))
540 (test-eval '(z 3 4) 15)
541 (test-eval '(cond ((= x -2) 'x=-2)
542 ((= x -25) 'x=-25)
543 (else 'failed))
544 'x=-25)
545 (test-eval '(if true false true) false)
546 (test-eval
547 '(let ((x 4) (y 7))
548 (+ x y (* x y)))
549 (+ 4 7 (* 4 7)))
552 ;; and/or
553 (geval '(define x (+ 3 8)))
554 (test-eval '(and 0 true x) 11)
555 (test-eval '(and 0 true x false) false)
556 (test-eval '(and 0 true x (set! x -2) false) false)
557 (test-eval 'x -2)
558 (test-eval '(and 0 true x false (set! x -5)) false)
559 (test-eval 'x -2)
560 (test-eval '(or false (set! x 25)) 'ok)
561 (test-eval 'x 25)
562 (test-eval '(or (set! x 2) (set! x 4)) 'ok)
563 (test-eval 'x 2)
564 (test-eval '(or false (set! x 25) true false) 'ok)
565 (test-eval 'x 25)
566 (test-eval '(or ((lambda (x) x) 5)) 5)
567 (test-eval '(or (begin (set! x (+ x 1)) x)) 26)
570 ;; cond
572 (test-eval
573 '(cond ((assoc 'b '((a 1) (b 2))) => cadr)
574 (else false))
575 2)
577 (test-eval
578 '(cond ((= 3 4) 'not-true)
579 ((= (* 2 4) 3) 'also-false)
580 ((map (lambda (x)
581 (* x (+ x 1)))
582 '(2 4 1 9))
583 =>
584 (lambda (x)
585 (accumulate + 0 x)))
586 (else 'never-reach))
587 118)
588 ;; '(6 20 2 90)
591 ;; procedure definition and application
592 (geval
593 '(define (factorial n)
594 (if (= n 0)
596 (* n (factorial (- n 1))))))
597 (test-eval '(factorial 5) 120)
599 ;; map
601 (test-eval
602 '(map (lambda (x)
603 (* x (+ x 1)))
604 '(2 1 4 2 8 3))
605 '(6 2 20 6 72 12))
606 ;; accumulate
608 (test-eval
609 '(accumulate + 0 '(1 2 3 4 5))
610 15)
612 ;; make-let
613 (test-eval
614 (make-let '(x y) '(3 5) '((+ x y)))
615 8)
616 (test-eval
617 '(let ()
618 5)
619 5)
620 (test-eval
621 '(let ((x 3))
622 x)
623 3)
624 (test-eval
625 '(let ((x 3)
626 (y 5))
627 (+ x y))
628 8)
629 (test-eval
630 '(let ((x 3)
631 (y 2))
632 (+ (let ((x (+ y 2))
633 (y x))
634 (* x y))
635 x y))
636 (+ (* 4 3) 3 2))
637 (test-eval
638 '(let ((x 6)
639 (y (let ((x 2))
640 (+ x 3)))
641 (z (let ((a (* 3 2)))
642 (+ a 3))))
643 (+ x y z))
644 (+ 6 5 9))
647 ;; let*
649 (test-eval
650 '(let* ((x 3)
651 (y (+ x 2))
652 (z (+ x y 5)))
653 (* x z))
654 39)
656 (test-eval
657 '(let* ()
658 5)
659 5)
660 (test-eval
661 '(let* ((x 3))
662 (let* ((y 5))
663 (+ x y)))
664 8)
666 (test-eval
667 '(let* ((x 3)
668 (y (+ x 1)))
669 (+ (let* ((x (+ y 2))
670 (y x))
671 (* x y))
672 x y))
673 (+ (* 6 6) 3 4))
674 (test-eval
675 '(let* ((x 6)
676 (y (let* ((x 2)
677 (a (let* ((x (* 3 x)))
678 (+ x 2))))
679 (+ x a)))
680 (z (+ x y)))
681 (+ x y z))
682 32)
684 ;; named-let
686 (test-eval
687 '(let eight ()
690 8)
691 8)
692 (test-eval
693 '(let loop ((count 0))
694 (if (= 100 count)
695 count
696 (loop (+ count 1))))
697 100)
698 (geval
699 '(define (prime? x)
700 (let prime-iter ((i 2))
701 (cond ((> (* i i) x) true)
702 ((= (remainder x i) 0) false)
703 (else (prime-iter (+ i 1)))))))
704 (test-eval
705 '(let primes ((x 2)
706 (n 20))
707 (cond ((= n 0) '())
708 ((prime? x)
709 (cons x
710 (primes (+ x 1) (- n 1))))
711 (else (primes (+ x 1) n))))
712 '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71))
714 (geval
715 '(define (fib n)
716 (let fib-iter ((a 1)
717 (b 0)
718 (count n))
719 (if (= count 0)
721 (fib-iter (+ a b) a (- count 1))))))
722 (test-eval '(fib 19) 4181)