week12 - CS 61A LAB EXERCISES = 3. Week 12 solutions Why...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 61A Week 12 solutions LAB EXERCISES ============= 3. Why doesn't make-procedure call eval? Because none of the arguments to lambda should be evaluated. In particular, the expressions that make up the body of the procedure are not evaluated until the procedure is *invoked*! 4.1, left-to-right (define (list-of-values exps env) ;; left to right (if (no-operands? exps) '() (let ((left (eval (first-operand exps) env))) (cons left (list-of-values (rest-operands exps) env))))) (define (list-of-values exps env) ;; right (if (no-operands? exps) '() (let ((right (list-of-values (rest-operands exps) env))) (cons (eval (first-operand exps) env) right)))) 4.2, Louis reordering (a) The trouble is that APPLICATION? cheats. The book has (define (application? exp) (pair? exp)) It really should be something like (define (application? exp) (and (pair? exp) (not (member (car exp) '(quote set! define if lambda begin cond))))) They get away with the shorter version precisely because EVAL doesn't call APPLICATION? until after it's checked for all the possible special forms. Louis (quite reasonably, I think) wants to rely on APPLICATION? behaving correctly no matter when it's called. (b) All we are changing is the syntax of an application, so we change the procedures that define the "application" abstract data type. These are on page 372 of the text. The new versions are: (define (application? exp) (tagged-list? exp 'call)) (define (operator exp) (cadr exp)) (define (operands exp) (cddr exp)) 4.4 AND and OR special forms The book suggests two solutions: make them primitive special forms
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
or make them derived expressions. We'll do both. As primitive special forms: Change the COND clause in EVAL by adding (cond . .. ((and? exp) (eval-and exp env)) ((or? exp) (eval-or exp env)) ...) (define (eval-and exp env) (define (iter tests) (cond ((null? tests) #t) ((null? (cdr tests)) (eval (car tests) env)) ((true? (eval (car tests) env)) (iter (cdr tests))) (else #f))) (iter (cdr exp))) (define (eval-or exp env) (define (iter tests) (if (null? tests) #f (let ((result (eval (car tests) env))) (if (true? result) result (iter (cdr tests)))))) (iter (cdr exp))) Now for the derived expression technique. Modify the COND clause in EVAL this way instead: (cond . .. ((and? exp) (eval (and->if (cdr exp)) env)) ((or? exp) (eval (or->if (cdr exp)) env)) ...) (define (and->if exps) (cond ((null? exps) #t) ((null? (cdr exps)) (car exps)) (else (make-if (car exps) (and->if (cdr exps)) #f)))) (define (or->if exps) (if (null? exps) #f (make-if (car exps) (car exps) (or->if (cdr exps))))) This version is elegant but has the disadvantage that you end up computing the first true value twice. 4.5 Cond => notation (define (expand-clauses clauses) (if (null? clauses)
Background image of page 2
'false (let ((first (car clauses)) (rest (cdr clauses))) (if (cond-else-clause? first) (if (null? rest) (sequence->exp (cond-actions first)) (error ". ..")) (IF (COND-ARROW-CLAUSE? FIRST) (LIST (MAKE-LAMBDA '(COND-FOO) (MAKE-IF 'COND-FOO (LIST (COND-ARROW-DOER FIRST)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

week12 - CS 61A LAB EXERCISES = 3. Week 12 solutions Why...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online