Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages 238-251.

Similar presentations


Presentation on theme: "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages 238-251."— Presentation transcript:

1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10

2 Environment Model 3.2, pages 238-251

3 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-rulelooking up the value of a variable define-rulecreating a new definition of a var set!-rulechanging the value of a variable lambda-rulecreating a procedure application rule applying a procedure The Environment Model

4 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 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

5 (define (square x) (* x x))

6 (square 5)

7 (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)))

8 Application: (f 5)

9 Nested Procedures (define g (lambda () (lambda (x y) (* x y)))) (define f (g)) (f 3 4) => 12

10 GE p: b:(lambda (x y) (* x y)) g: (define g (lambda () (lambda (x y) (* x y))))

11 GE p: b:(lambda (x y) (* x y)) g: f: p: x y b: (* x y) E1 empty (define f (g))

12 GE p: b:(lambda (x y) (* x y)) g: f: p: x y b: (* x y) E1 empty (f 3 4) X=3 Y=4 E2

13 Nested Procedures (define g (lambda (z) (lambda (x y) (* x y z)))) (define f (g 2)) (f 3 4) => 24

14 GE p: z b:(lambda (x y) (* x y z)) g: f: (define g (lambda (z) (lambda (x y) (* x y z))))

15 GE p: z b:(lambda (x y) (* x y z)) g: f: p: x y b: (* x y z) E1 Z: 2 (define f (g 2))

16 GE p: z b:(lambda (x y) (* x y z)) g: f: p: x y b: (* x y z) E1 Z: 2 (f 3 4) X=3 Y=4 E2

17 Let expressions (let (( )) ) is syntactic sugar for ((lambda ( ) ) ) (define a 5) (define b 6) (let ((a 2) (c a)) (+ a b c)) = ((lambda (a c) (+ a b c)) 2 a)

18 Let – cont. GE a: 5b: 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)

19 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 #

20 (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))

21 (define W1 (make-withdraw 100))

22 (W1 50)

23

24 More than one cash machine >(define W1 (make-withdraw 100)) >(define W2 (make-withdraw 100)) > >(W1 50) > >(W2 40) > 50 60

25 (define W2 (make-withdraw 100))

26 question from past exams 26

27 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

28 make-line: a: 4 b: 5 proc: GE p: a b b:(lambda (x)… a: 1 b: 2 E1 p: x b:(cond…

29 make-line: a: 3 b: 5 proc: GE p: a b b:(lambda (x)… a: 1 b: 2 E1 p: x b:(cond… x: 1 E2 (+ b (* x a)) (set! a (proc 1))

30 make-line: a: 3 b: 5 proc: GE p: a b b:(lambda (x)… a: 1 3 b: 2 4 E1 p: x b:(cond… x: 3.4 E3 (set! a (car x)) (set! b (cdr x)) (proc (cons 3 4))

31 make-line: a: 3 b: 5 proc:c: 7 GE p: a b b:(lambda (x)… a: 3 b: 4 E1 p: x b:(cond… x: 1 E4 (+ b (* x a)) (define c (proc 1))


Download ppt "מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages 238-251."

Similar presentations


Ads by Google