hw12 - CS 61A HOMEWORK = 4.3 Week 12 solutions...

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

View Full Document Right Arrow Icon
CS 61A Week 12 solutions HOMEWORK ======== 4.3 data-directed eval The table itself could be done in several ways; perhaps the easiest is to use the built-in table from chapter 2. So we say: (put 'quote 'eval text-of-quotation) (put 'define 'eval eval-definition) (put 'set! 'eval eval-assignment) Where the original eval does something other than (foo exp env) we have to write an interface procedure. For example: (define (eval-lambda exp env) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) (put 'lambda 'eval eval-lambda) (define (eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) (else (let ((form (get (operator exp) 'eval))) (if form ;; IT'S A SPECIAL FORM (form exp env) ;; SO form IS THE PROCEDURE TO CALL (apply (eval (operator exp) env) (list-of-values (operands exp) env) )))))) The first two COND clauses deal with atomic expressions: numbers (which are self-evaluating) and symbols (which represent variables). If the expression is neither of those, then it's a list, and we look at its CAR. We look that up in the table; if we find it, the expression is a special form, and we invoke the particular procedure that knows about that special form. Otherwise, it's a regular procedure. We're neglecting various kinds of errors that might occur with mal-formed input. We also have to rewrite text-of-quotation so that it accepts an extra input, the environment, even though it doesn't need it: (define (text-of-quotation exp env) (cadr exp)) And we have to write a new "front end" to cond->if: (define (eval-cond exp env) (eval (cond->if exp) env)) and put that in the table. It would also be possible to include the atomic expressions in the general data-directed mechanism by assigning them implicit types just as we assigned Scheme numbers an implicit type in exercise 2.78, page 193:
Background image of page 1

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

View Full Document Right Arrow Icon
(define (expression-type exp) (cond ((self-evaluating? exp) '(() SELF-EVALUATING)) ((symbol? exp) '(() SYMBOL)) ((pair? exp) (car exp)) (else (error "Unknown expression type" exp)))) (define (eval exp env) (let ((handler (get (expression-type exp) 'eval))) (if handler (handler exp env) (apply (eval (operator exp) env) (list-of-values (operands exp) env))))) (put '(() self-evaluating) 'eval (lambda (exp env) exp)) (put '(() symbol) 'eval lookup-variable-value) The reason for using (() SYMBOL) instead of just SYMBOL as the type tag is that otherwise we'd get in trouble if an expression tried to call a procedure named SYMBOL. These type tags aren't valid Scheme expressions, so they shouldn't get us in trouble. 4.6 Implementing LET ;; In eval's big cond we put ((let? exp) (eval (let->combination exp) env)) ;; Now for the guts of the problem: (define (let->combination exp) (cons (make-lambda (let-formals exp) (let-body exp)) (let-actuals exp))) ;; And now for the data abstraction stuff: (define (let? exp) (tagged-list? exp 'let)) (define (let-formals exp) (map car (cadr exp))) (define (let-actuals exp) (map cadr (cadr exp))) (define (let-body exp)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 13

hw12 - CS 61A HOMEWORK = 4.3 Week 12 solutions...

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

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