Last active
November 20, 2015 05:00
-
-
Save pablq/c8fd01ead9f83b4e1f2e to your computer and use it in GitHub Desktop.
notes from lecture 7a of Structure and Interpretation of Computer Programs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; purely for effect --- this is not a practical implementation | |
(define eval | |
(lambda (expression environment) | |
(cond ((number? expression) expression) | |
((symbol? expression) (lookup expression environment)) | |
((eq? (car expression) 'quote) (cadr expression)) | |
((eq? (car expression) 'lambda) | |
(list 'closure (cdr expression) environment)) | |
((eq? (car expression) 'cond) | |
(evcond (cdr expression) environment)) | |
(else (apply (eval (car expression) environment) | |
(evlist (cdr expression) environment)))))) | |
;; 'lambda > '( 'closure (((args) body) <environment> )) | |
(define apply | |
(lambda (procedure arguments) | |
(cond ((primitive? procedure) | |
(apply_primop procedure arguments)) | |
((eq? (car procedure) 'closure) | |
(eval (cadadr procedure) | |
(bind (caadr procedure) | |
arguments | |
(caddr procedure)))) | |
(else error)))) | |
;; not implemented | |
;; primitive? | |
;; apply_primop | |
;; bind | |
(define evlist | |
(lambda (l environment) | |
(cond ((eq? l '()) '()) | |
(else | |
(cons (eval (car l) environment) | |
(evlist (cdr l) environment)))))) | |
;; caar -> (( * _ ) _) | |
;; cad-ar -> ((a b) (b (c d))) -> b | |
(define evcond | |
(lambda (clauses environment) | |
(cond ((eq? clauses '()) '()) | |
((eq? (caar clauses) 'else) | |
(eval (cadar clauses) environment)) | |
((false? (eval (caar clauses) environment) | |
(evcond (cdr clauses) environment))) | |
(else | |
(eval (cadar clauses) environment))))) | |
;; bind a list symbol-value pairs to an environment | |
(define bind | |
(lambda (vars vals env) | |
(cons (pair-up vars vals) | |
env))) | |
;; take a list of symbols and a list of values and create a list of dotted pairs with it. | |
(define pair-up | |
(lambda (vars vals) | |
(cond | |
((eq? vars '()) | |
(cond ((eq? vals '()) '()) | |
(else (error 'TOOMANYARGUMENTS)))) | |
((eq? vals? '()) | |
(error 'TOOFEWARGUMENTS)) | |
(else | |
(cons (cons (car vars) (car vals)) | |
(pair-up (cdr vars) (cdr vals))))))) | |
;; get a symbol and run through environment to find the value that matches it... | |
;; it will look in the next frame if the local env doesn't have a match | |
(define lookup | |
(lambda (sym env) | |
(cond ((eq? env '()) (error UNBOUNDVARIABLE)) | |
(else | |
((lambda (vcell) | |
(cond ((eq? vcell '()) | |
(lookup sym (cdr env))) | |
(else (cdr vcell)))) | |
(assq sym (car env))))))) | |
;; search list of dotted pairs for the pair where the first item matches the symbol | |
(define assq | |
(lambda (sym alist) | |
(cond ((eq? (caar alist) '()) '()) | |
((eq? (caar alist) sym) (car alist)) | |
(else | |
(assq sym (cdr alist)))))) | |
;; eval >>> procedure + arguments >>> apply | |
;; apply >>> expression + environment >>> eva | |
;; | |
;; eval > evlist > eval | |
;; eval > evcond > eval | |
;; apply > apply_primop > apply | |
;; | |
;; (cadadr '(closure ((params) exp) env)) -> exp | |
;; '(closure ((params) exp) env) | |
;; (((params) exp) env) | |
;; ((params) exp) | |
;; (exp) | |
;; exp | |
;; | |
;; (cadar '((a b c) d e)) -> b | |
;; '((a b c) d e) | |
;; (a b c) | |
;; (b c) | |
;; b | |
;; | |
;; CDR ALWAYS RETURNS A LIST!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment