Presentation is loading. Please wait.

Presentation is loading. Please wait.

Environment Model Examples Prof. Tony White April 2010 Ref:

Similar presentations


Presentation on theme: "Environment Model Examples Prof. Tony White April 2010 Ref:"— Presentation transcript:

1 Environment Model Examples Prof. Tony White April 2010 Ref: http://www.cs.caltech.edu/courses/cs1/resources/environment-model-tutorial/environments.html

2 Example 1 fooλ(1) foo’ x2 1. (define (foo x) (+ x 1)) 2. (foo 2) Π=3 Π=1

3 Example 2 let’ x1 y2 Π=3 1.(let ((x 1) 2. (y 2)) 3. (+ x y)) Π=4

4 Example 3 lambda’ x1 y2 Π=1 1. (lambda (x y) (+ x y)) 1 2) Π=2

5 Example 4-a sqrtfλ(1) incλ(3) Π=4 1.(define (sqrtf f) 2. (lambda (x) (sqrt (f x)))) 3.(define (inc n) (+ n 1)) 4.(define f1 (sqrtf inc)) 5.(f1 3)

6 Example 4-b sqrtfλ(1) incλ(3) f1λ(2) sqrtf’ fλ(3) Π=5 1.(define (sqrtf f) 2. (lambda (x) (sqrt (f x)))) 3.(define (inc n) (+ n 1)) 4.(define f1 (sqrtf inc)) 5.(f1 3)

7 Example 4-c sqrtfλ(1) incλ(3) f1λ(2) sqrtf’ fλ(3) Π=6 x3 f1’ Π=2 1.(define (sqrtf f) 2. (lambda (x) (sqrt (f x)))) 3.(define (inc n) (+ n 1)) 4.(define f1 (sqrtf inc)) 5.(f1 3)

8 Example 4-d sqrtfλ(1) incλ(3) f1λ(2) sqrtf’ fλ(3) Π=6 x3 f1’ Π=2 n3 f’ Π=3 1.(define (sqrtf f) 2. (lambda (x) (sqrt (f x)))) 3.(define (inc n) (+ n 1)) 4.(define f1 (sqrtf inc)) 5.(f1 3)

9 Example 5-a barλ(4) Π=5 let’ x1 y2 1.(define bar 2. (let ((x 1) 3. (y 2)) 4. (lambda (z) (+ x y z)))) 5.(bar 3)

10 Example 5-b barλ(4) Π=6 let’ x1 y2 z3 bar’ Π=4 1.(define bar 2. (let ((x 1) 3. (y 2)) 4. (lambda (z) (+ x y z)))) 5.(bar 3)

11 Example 6: Code 1.(define (foo z) 2. (let ((x z)) 3. (let ((y (+ x z))) 4. (lambda (sym) 5. (cond ((eq? sym 'x) x) 6. ((eq? sym 'y) y) 7. ((eq? sym 'bump-x) (set! x (+ x z))) 8. ((eq? sym 'bump-y) (set! y (+ y z))) 9. ((eq? sym 'reset-x) (set! x 0)) 10. ((eq? sym 'reset-y) (set! y 0))))))) 11. (define f (foo 10)) 12. (f 'x) 13. (f 'y) 14. (f 'bump-x) 15. ; Rest of code not shown

12 Example 6-a fooλ(1) fλ(3) Π=12 z10 x y20 foo’

13 Example 6-b fooλ(1) fλ(3) Π=13 z10 x y20 foo’ sym‘x Π=5

14 Example 6-c fooλ(1) fλ(3) Π=15 z10 x20 y foo’ sym‘bump-x Π=7

15 Example 6-d Rest of code associated with example 6 left as an exercise to the reader. It really is a set of variations on 6-b and 6-c.

16 Example 7: Code 1.(define (new-sqrt x) 2. (define (good-enough? guess) 3. (< (abs (- (square guess) x)) 0.000001)) 4. (define (average x y) 5. (/ (+ x y) 2)) 6. (define (improve guess) 7. (average guess (/ x guess))) 8. (define (sqrt-iter guess) 9. (if (good-enough? guess) 10. guess 11. (sqrt-iter (improve guess)))) 12. (sqrt-iter 1.0)) 13. (new-sqrt 2.0)

17 Example 7-a new-sqrtλ( 1) Π=13

18 Example 7-b new-sqrtλ( 1) Π=14 new-sqrt’ x2.0 good-enough?λ( 2) averageλ( 4) improveλ( 6) sqrt-iterλ( 8) Π=12

19 Example 7-c new-sqrtλ( 1) Π=14 new-sqrt’ x2.0 good-enough?λ( 2) averageλ( 4) improveλ( 6) sqrt-iterλ( 8) Π=12 guess1.0 sqrt-iter’ Π=9

20 Example 8-a fooλ( 1) Π=5 1.(define (foo x) 2. (let ((y (lambda (w) (+ x w))) 3. (z (lambda (w) (* x w)))) 4. (y (z x)))) 5.(foo 10)

21 Example 8-b fooλ( 1) Π=6 foo’ x10 Π=2 1.(define (foo x) 2. (let ((y (lambda (w) (+ x w))) 3. (z (lambda (w) (* x w)))) 4. (y (z x)))) 5.(foo 10)

22 Example 8-c fooλ( 1) Π=6 foo’ x10 Π=2 let’ yλ( 2) zλ( 3) Π=4 1.(define (foo x) 2. (let ((y (lambda (w) (+ x w))) 3. (z (lambda (w) (* x w)))) 4. (y (z x)))) 5.(foo 10)


Download ppt "Environment Model Examples Prof. Tony White April 2010 Ref:"

Similar presentations


Ads by Google