DOC PREVIEW
MIT 6 001 - Recitation 18 Interpretation

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

6.001, Fall 2007—Recitation 18 — 11/7/2007 1MASSACHVSETTS INSTITVTE OF TECHNOLOGYDepartment of Electrical Engineering and Computer Science6.001—Structure and Interpretation of Computer ProgramsFall 2007Recitation 18 — 11/7/2007InterpretationInterpreter code from lecture, with only a few minor modifications. This version still uses almostno abstractions.(define (eval exp env)(cond ((number? exp) exp)((symbol? exp) (lookup exp env))((eq? (car exp) ’quote) (cadr exp))((eq? (car exp) ’cond)(eval-clauses (cdr exp) env))((eq? (car exp) ’lambda)(eval-lambda exp env))(else(apply (eval (car exp) env)(values (cdr exp) env)))))(define (apply proc args)(cond ((primitive? proc) (papply proc args))((eq? (car proc) ’procedure)(let ((parameters (cadr proc))(body (caddr proc))(env (cadddr proc)))(eval body(extend envparametersargs))))(else (error "not a proc"))))(define (eval-lambda exp env)(let ((parameters (cadr exp))(body (caddr exp)))(list ’procedure parameters body env)))(define (values exps env)(cond ((null? exps) ’())(else (cons (eval (car exps) env)(values (cdr exps) env)))))(define (eval-clauses clauses env)(cond ((null? clauses) ’())((eq? (caar clauses) ’else)(eval (cadar clauses) env))((not (eq? (eval (caar clauses) env) #f))(eval (cadar clauses) env))(else (eval-clauses (cdr clauses) env))))6.001, Fall 2007—Recitation 18 — 11/7/2007 2(define (extend base-env vars values)(cons (make-frame vars values) base-env))(define (make-frame vars vals)(cond ((null? vars) (cond ((null? vals) ’())(else (error "too many args"))))((null? vals) (error "too few args"))(else (cons (cons (car vars) (car vals))(make-frame (cdr vars) (cdr vals))))))(define (lookup var env)(cond ((null? env) (error "unbound variable" var))(else (let ((binding (assq var (car env))))(cond (binding (cdr binding))(else (lookup var (cdr env))))))))(define (assq var bindings)(cond ((null? bindings) #f)((eq? (caar bindings) var) (car bindings))(else (assq var (cdr bindings)))))(define (primitive? proc)(memq proc (list car cdr cons eq? + = > <)))(define (papply proc args)(cond ((eq? proc car) (car (car args)))((eq? proc cdr) (cdr (car args)))((eq? proc cons) (cons (car args) (cadr args)))((eq? proc eq?) (eq? (car args)))((eq? proc +) (+ (car args) (cadr args)))((eq? proc >) (> (car args) (cadr args)))))ProblemsSuppose the global environment is defined as such:(define *GE*(extend ’()’(car cdr cons eq? + >)(list car cdr cons eq? + >)))Evaluate the following, in order1. (eval ’x *GE*)6.001, Fall 2007—Recitation 18 — 11/7/2007 32. (eval ’(eval ’(> x 4)(extend *GE*’(x)(list 3)))3. (eval ’((lambda (x) (+ x 2)) 5) *GE*)4. (eval ’(cond ((> x 4) (quote a))((> 1 x) (quote b))(else (quote c)))(extend *GE*’(x)(list 2)))5. Add a new special form: begin(define (eval-begin exp env)6.001, Fall 2007—Recitation 18 — 11/7/2007 46. What other standard special forms are missing? How would you add


View Full Document

MIT 6 001 - Recitation 18 Interpretation

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Recitation 18 Interpretation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Recitation 18 Interpretation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Recitation 18 Interpretation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?