310 likes | 322 Views
מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 10. Environment Model. 3.2, pages 238-251. Environments. Binding: a pairing of a name and a value Frame : a table of bindings Environment : a sequence of frames. The Environment Model.
E N D
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10
Environment Model 3.2, pages 238-251
Environments • Binding: a pairing of a name and a value • Frame: a table of bindings • Environment: a sequence of frames The Environment Model • 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 • Name-rule: A name X evaluated in environment E givesthe 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 A 2. 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)))
Nested Procedures (define g (lambda () (lambda (x y) (* x y)))) (define f (g)) (f 3 4) => 12
GE g: p:b:(lambda (x y) (* x y)) (define g (lambda () (lambda (x y) (* x y))))
f: GE g: E1 empty p:b:(lambda (x y) (* x y)) p: x yb: (* x y) (define f (g))
f: GE g: E1 empty p:b:(lambda (x y) (* x y)) X=3 Y=4 E2 p: x yb: (* x y) (f 3 4)
Nested Procedures (define g (lambda (z) (lambda (x y) (* x y z)))) (define f (g 2)) (f 3 4) => 24
f: GE g: p: zb:(lambda (x y) (* x y z)) (define g (lambda (z) (lambda (x y) (* x y z))))
f: GE g: E1 Z: 2 p: zb:(lambda (x y) (* x y z)) p: x yb: (* x y z) (define f (g 2))
f: GE g: E1 Z: 2 p: zb:(lambda (x y) (* x y z)) X=3 Y=4 E2 p: x yb: (* x y z) (f 3 4)
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)
E1 a: 2 c: 5 p: a c b: (+ a b c) Let – cont. GE a: 5 b: 6 (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) > #<procedure> 50 10 Insufficient funds
(define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))(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
Q1 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))
make-line: a: 4 b: 5 proc: GE a: 1 b: 2 E1 p: a bb:(lambda (x)… p: x b:(cond…
make-line: a: 3 b: 5 proc: GE a: 1 b: 2 E1 p: a bb:(lambda (x)… E2 x: 1 p: x b:(cond… (+ b (* x a)) (set! a (proc 1))
make-line: a: 3 b: 5 proc: GE a: 1 3 b: 2 4 E1 p: a bb:(lambda (x)… E3 x: 3.4 p: x b:(cond… (set! a (car x)) (set! b (cdr x)) (proc (cons 3 4))
make-line: a: 3 b: 5 proc: c: 7 GE a: 3 b: 4 E1 p: a bb:(lambda (x)… E4 x: 1 p: x b:(cond… (+ b (* x a)) (define c (proc 1))