Post on 19-Dec-2015
Environments• Binding: a pairing of a name and a value
• Frame: a table of bindings
• Environment: a sequence of frames
• A precise, completely mechanical, description of:• name-rule looking up the value of a variable• define-rule creating a new definition of a var• set!-rule changing the value of a
variable• lambda-rule creating a procedure• application rule applying a procedure
The Environment Model
The Environment Model• Name-rule: A name X evaluated in environment E gives
the value of X in the first frame of E where X is bound• Define-rule: A define special form evaluated in environment E creates
or replaces a binding in the first frame of E• Set!-rule: A set! of variable X evaluated in environment E changes the
binding of X in the first frame of E where X is bound• Lambda-rule: A lambda special form evaluated in environment E
creates a procedure whose environment pointer points to E• Application Rule: To apply a compound procedure P to arguments
1.Create a new frame A2. Make A into an environment E:
A's enclosing environment pointer goes to the same frame as the environment pointer of P
3. In A, bind the parameters of P to the argument values
4. Evaluate the body of P with E as the current environment
(define (square x) (* x x))(define (sum-of-squares x y) (+ (square x) (square y)))(define (f a) (sum-of-squares (+ a 1) (* a 2)))
Let expressions
(let ((<var> <exp>)) <body>)is syntactic sugar for((lambda (<var>) <body>) <exp>)
(define a 5)(define b 6)(let ((a 2) (c a))
(+ a b c))=((lambda (a c) (+ a b c)) 2 a)
Let – cont.GE
a: 5 b: 6
p: a c
b: (+ a b c)
E1
a: 2
c: 5
(define a 5)(define b 6)(let ((a 2) (c a))
(+ a b c))=((lambda (a c) (+ a b c)) 2
a)
The cash machine(define (make-withdraw balance)
(lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))
(define W1 (make-withdraw 100))
> W1
>
>(W1 50)
>
>(W1 40)
>
>(W1 20)
>
50
10
Insufficient funds
#<procedure>
(define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))
More than one cash machine>(define W1 (make-withdraw 100))
>(define W2 (make-withdraw 100))
>
>(W1 50)
>
>(W2 40)
>
50
60
Make-line
(define (make-line a b) (lambda (x) (cond ((pair? x) (set! a (car x)) (set! b (cdr x)))
(else (+ b (* x a)))) ) ) (define a 4)(define b 5)(define proc (make-line 1 2))
Q1
make-line:a: 3b: 5proc:
GE
p: a bb:(lambda (x)…
a: 1b: 2
E1
p: xb:(cond…
x: 1E2
(+ b (* x a))(set! a (proc 1))
make-line:a: 3b: 5proc:
GE
p: a bb:(lambda (x)…
a: 1 3b: 2 4
E1
p: xb:(cond…
x: 3.4E3
(set! a (car x))(set! b (cdr x))(proc (cons 3 4))
make-line:a: 3b: 5proc: c: 7
GE
p: a bb:(lambda (x)…
a: 3b: 4
E1
p: xb:(cond…
x: 1E4
(+ b (* x a))(define c (proc 1))
(
(lambda (z)
(define x (lambda (x) (lambda (y z) (y x))))
( ( (x (lambda () z)) (lambda (z) z) 3 ) )
)
2)
Expression Env Comment
1 ((lambda1(z) op1 op2) 2) GE (op1 op2)
2 (op1 op2) E1
3 Op1(define x (lambda2 (x) (lambda3(y z) (y x))))
E1
4 Op2(((x (lambda5 () z)) (lambda4(z) z) 3))((op3 (lambda4 (z) z) 3))
E1
5 op3(x (lambda5 () z))
E1 l3
6 ((l3 l4 3)) E1
7 (y x) E3 (l4 l5)
8 Z E4 l5
9 (l5) E1
10 Z E5 2