; ACT ONE -- "let", scope, shadowing ; Thanks to Cherian Mathew ; Three ways to compute the sum of pi and e (let ((pi 3.14159) (e 2.71828)) (+ pi e)) ((lambda (pi e) (+ pi e)) 3.14159 2.71828) (define body (lambda (pi e) (+ pi e))) (body 3.14159 2.71828) ; A puzzle: What is the result of (f '(a b c)) (define i 0) (define f (lambda (lat) (cond ((null? lat) (cons i '())) (else (let ((i (+ i 1))) (let ((i (+ i 1))) (cons i (f (cdr lat))))))))) ; ACT TWO -- defunctionalizing the procedural representation of ; environments (EOPL Section 2.2.3) into a data-structure ; representation of environments (EOPL Section 2.2.2). Compare ; the two implementations in the book and see how they correspond ; to each other! (define-datatype env env? (way1) (way2 (saved-sym symbol?) (saved-val (lambda (x) #t)) (saved-env env?))) (define empty-env (lambda () (way1)))) (define extend-env (lambda (saved-sym saved-val saved-env) (way2 saved-sym saved-val saved-env))) (define apply-env (lambda (e sym) (cases env e (way1 () (eopl:error 'empty-env "~s not found in environment" sym)) (way2 (saved-sym saved-val saved-env) (if (eq? sym saved-sym) saved-val (apply-env saved-env sym))))))