1 / 31

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

מבוא מורחב למדעי המחשב בשפת 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.

dlucas
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

  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 g: p:b:(lambda (x y) (* x y)) (define g (lambda () (lambda (x y) (* x y))))

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

  12. 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)

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

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

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

  16. 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)

  17. 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)

  18. 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)

  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) > #<procedure> 50 10 Insufficient funds

  20. (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")))

  21. (define W1 (make-withdraw 100))

  22. (W1 50)

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

  24. (define W2 (make-withdraw 100))

  25. question from past exams

  26. 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))

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

  28. 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))

  29. 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))

  30. 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))

More Related