Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2...
-
Upload
shonda-armstrong -
Category
Documents
-
view
214 -
download
0
Transcript of Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2...
![Page 1: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/1.jpg)
Fall 2008 Programming Development Techniques
1
Topic 18 Environment Model of
Evaluation
Section 3.2
Acknowledgement: This lecture (and also much of the previous one) were taken from an old CalTech
Course Page
![Page 2: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/2.jpg)
Fall 2008 Programming Development Techniques
2
bindings (review)
• a binding is an association between a name and a Scheme value
• names:– variable names, procedure names– formal parameters of procedures– in let statements: (let ((name value) ...) ...)
• values: any Scheme value
![Page 3: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/3.jpg)
Fall 2008 Programming Development Techniques
3
bindings (review)
• a binding is an association between a name and a Scheme value
• examples:– name: x value: 10– name: y value: #f– name: square value: (lambda (x) (* x x))
![Page 4: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/4.jpg)
Fall 2008 Programming Development Techniques
4
frames (review)
• a frame is a collection of bindings:
x: 10
y: #f
square: (lambda (x) (* x x))
![Page 5: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/5.jpg)
Fall 2008 Programming Development Techniques
5
frames (review)
• frames are used to look up the value corresponding to a name– x = ?– y = ?– square = ?
x: 10
y: #f
square: (lambda (x) (* x x))
![Page 6: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/6.jpg)
Fall 2008 Programming Development Techniques
6
frames (review)
• frames have an enclosing environment– which will be another
frame (parent frame)– if lookup fails, go to parent
frame and try again– then to its parent frame
etc. x: 10
y: #f
square: (lambda (x) (* x x))
to parent frame
![Page 7: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/7.jpg)
Fall 2008 Programming Development Techniques
7
environments (review)
• an environment is a linked chain of frames ending in the global environment
• every frame defines an environment starting from it
x: 10
y: 20
z: 30
(global environment)
E1
E2
E3
![Page 8: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/8.jpg)
Fall 2008 Programming Development Techniques
8
environments (review)
• the global environment is there when Scheme interpreter starts up
• evaluating code can create new frames– and thus new environments
• all code being evaluated does so in the context of an environment– because names must be looked up– called the current environment
![Page 9: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/9.jpg)
Fall 2008 Programming Development Techniques
9
rule 1: define
• define creates a new binding in the current environment (current frame)
• example:• (define x 10)
(global environment)
![Page 10: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/10.jpg)
Fall 2008 Programming Development Techniques
10
rule 1: define
• define creates a new binding in the current environment (current frame)
• example:• (define x 10)
(global environment)
x: 10
![Page 11: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/11.jpg)
Fall 2008 Programming Development Techniques
11
rule 2: set!
• set! changes an old binding in the current environment
• NEVER creates a new binding• example:• (set! x 'foo)
(global environment)
x: 10
![Page 12: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/12.jpg)
Fall 2008 Programming Development Techniques
12
rule 2: set!
• set! changes an old binding in the current environment
• NEVER creates a new binding• example:• (set! x 'foo)
(global environment)
x: foo
![Page 13: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/13.jpg)
Fall 2008 Programming Development Techniques
13
rule 2: set!
• set! can change a binding in a different frame (if no binding in current frame)
• example:• (set! x 24)
– no x in current env– so change in global env
(global environment)x: foo
y: 13
(current environment)
![Page 14: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/14.jpg)
Fall 2008 Programming Development Techniques
14
rule 2: set!
• set! can change a binding in a different frame (if no binding in current frame)
• example:• (set! x 24)
– no x in current env– so change in global env
(global environment)x: 24
y: 13
(current environment)
![Page 15: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/15.jpg)
Fall 2008 Programming Development Techniques
15
rule 3: lambda
• a lambda expression (procedure) is a pair:– the text of the lambda expression– a pointer to the environment in which the lambda expression
was evaluated (created)
![Page 16: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/16.jpg)
Fall 2008 Programming Development Techniques
16
rule 3: lambda
• evaluate:
(lambda (x) (* x x))
global environment
params: x
code: (* x x)
pointer toenvironmentwhere lambdaevaluated
text of lambda
![Page 17: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/17.jpg)
Fall 2008 Programming Development Techniques
17
with define
• (define square
(lambda (x) (* x x)))
global environment
params: x
code: (* x x)
square:
![Page 18: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/18.jpg)
Fall 2008 Programming Development Techniques
18
rule 4: evaluation
• changes the way we model apply• To apply a procedure:
1. construct a new frame
2. bind the formal parameters of the procedure to the arguments of the procedure call
3. the new frame’s parent is the environment associated with the called procedure
• not the calling procedure
4. evaluate the body in the new environment
![Page 19: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/19.jpg)
Fall 2008 Programming Development Techniques
19
evaluating (square 10)
global environment
params: x
code: (* x x)
square:
![Page 20: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/20.jpg)
Fall 2008 Programming Development Techniques
20
evaluating (square 10)
global environment
params: x
code: (* x x)
square:
new
frame
current environment
![Page 21: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/21.jpg)
Fall 2008 Programming Development Techniques
21
evaluating (square 10)
global environment
params: x
code: (* x x)
square:
new
frame
current environment
x: 10
![Page 22: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/22.jpg)
Fall 2008 Programming Development Techniques
22
evaluating (square 10)
global environment
params: x
code: (* x x)
square:
current env
evaluate: (* x x)
x: 10
![Page 23: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/23.jpg)
Fall 2008 Programming Development Techniques
23
evaluating (square 10)
global environment
params: x
code: (* x x)
square:
current env
(* x x) =>
(* 10 10) => 100
x: 10
![Page 24: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/24.jpg)
Fall 2008 Programming Development Techniques
24
evaluating (square 10)
global environment
params: x
code: (* x x)
square: current env
result: 100
new frame "goes away"
![Page 25: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/25.jpg)
Fall 2008 Programming Development Techniques
25
That was WAY too easy!
Let's try something harder...
![Page 26: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/26.jpg)
Fall 2008 Programming Development Techniques
26
accumulator (revisited)
• (define (make-accum value)
(lambda (x)
(set! value (+ value x))
value))
• N.B. body of lambda, define, cond clause is implicitly a begin block.
![Page 27: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/27.jpg)
Fall 2008 Programming Development Techniques
27
accumulator• (define (make-accum value)
(lambda (x)
(set! value (+ value x))
value)) current env
![Page 28: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/28.jpg)
Fall 2008 Programming Development Techniques
28
accumulator (another look)
• (define make-accum
(lambda (value)
(lambda (x)
(set! value
(+ value x))
value))
current env
![Page 29: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/29.jpg)
Fall 2008 Programming Development Techniques
29
accumulator
• (define a! (make-accum 0))– evaluate (make-accum 0)
• create frame
• bind arguments
current env
![Page 30: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/30.jpg)
Fall 2008 Programming Development Techniques
30
accumulator• evaluate (make-accum 0)
– create, bind– evaluate body
• (lambda (x) …)
current env
![Page 31: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/31.jpg)
Fall 2008 Programming Development Techniques
31
rule 3:
• when we create a procedure (evaluate a lambda expression)– its environment is the environment in which the lambda
expression is evaluated
![Page 32: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/32.jpg)
Fall 2008 Programming Development Techniques
32
accumulator• evaluate (make-accum 0)
– create, bind– evaluate body
• (lambda (x) …)
– result is lambda• with env binding
current env
![Page 33: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/33.jpg)
Fall 2008 Programming Development Techniques
33
accumulator
• (define a! (make-accum 0))– evaluate (make-accum 0)
• result is (lambda (x) …) with env ptr
– make binding for a! • in its environment
current env
![Page 34: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/34.jpg)
Fall 2008 Programming Development Techniques
34
after (define a!…)
current env
![Page 35: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/35.jpg)
Fall 2008 Programming Development Techniques
35
after (define a!…)
"trapped" frame persists
current env
![Page 36: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/36.jpg)
Fall 2008 Programming Development Techniques
36
using a!• (a! 1)
– create frame– bind values
current env
![Page 37: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/37.jpg)
Fall 2008 Programming Development Techniques
37
using a!
• (a! 1)– create frame– bind values– evaluate body
• (begin (set! value…) …)– (set! value (+ x value))– (set! value (+ 1 0))– (set! value 1)
current env
![Page 38: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/38.jpg)
Fall 2008 Programming Development Techniques
38
using a!
• (a! 1)– Create frame– Bind values– Evaluate body
• (begin (set! value…) ,,,,)
• (set! value (+ x value))
• (set! value (+ 1 0))
• (set! value 1)
current env
![Page 39: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/39.jpg)
Fall 2008 Programming Development Techniques
39
environment after (a! 1)
trapped frame
current env
![Page 40: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/40.jpg)
Fall 2008 Programming Development Techniques
40
more usage
• (a! 1)
2• (a! 1)
3
![Page 41: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/41.jpg)
Fall 2008 Programming Development Techniques
41
accumulator 2
• (define b! (make-accum 0))– evaluate (make-accum 0)
• create
• bind…
current env
![Page 42: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/42.jpg)
Fall 2008 Programming Development Techniques
42
accumulator 2
• (define b! (make-accum 0))– evaluate (make-accum 0)
• result is (lambda (x) …) w/ env ptr
– make binding for b!
current env
![Page 43: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/43.jpg)
Fall 2008 Programming Development Techniques
43
resulting environment• a! and b! have their own value
current env
![Page 44: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/44.jpg)
Fall 2008 Programming Development Techniques
44
using b!
• (b! 1)– …frame
current env
![Page 45: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/45.jpg)
Fall 2008 Programming Development Techniques
45
using b!
• (b! 1)– …frame– …set!
current env
![Page 46: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/46.jpg)
Fall 2008 Programming Development Techniques
46
using a! again
• (a! 1)– …frame
current env
![Page 47: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/47.jpg)
Fall 2008 Programming Development Techniques
47
using a! again
• (a! 1)– …frame– …set!
current env
![Page 48: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/48.jpg)
Fall 2008 Programming Development Techniques
48
using b!
• (b! 1) 2• (a! 1) 3• (a! 1) 4• (b! 1) 3
![Page 49: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/49.jpg)
Fall 2008 Programming Development Techniques
49
procedure environment• make-accum
creates a unique frame for each call
• the lambda traps the frame and keeps a pointer to it
• only that lambda has access to that frame
![Page 50: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/50.jpg)
Fall 2008 Programming Development Techniques
50
procedure environment• value is a local variable that is owned (and
accessible) exclusively by the resulting lambda
![Page 51: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/51.jpg)
Fall 2008 Programming Development Techniques
51
a! and b!
• a! and b! have unique procedure frames• have unique local variables: value• don’t affect each other• remember own state• important form of encapsulation
– limits unwanted interactions
![Page 52: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/52.jpg)
Fall 2008 Programming Development Techniques
52
contrast
• (define astate 0)• (define (accum! x)
(set! astate (+ x astate)) astate))
• (define (toggle!) (if (= astate 0) (set! astate 1) (set! astate 0)))
![Page 53: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/53.jpg)
Fall 2008 Programming Development Techniques
53
interaction
• (accum! 1) 1• (toggle!) ;; astate: 0• (accum! 1) 1• (accum! 1) 2
• (toggle!) ;; astate: 0• (accum! 1) 1
• toggle! and accum! share a variable
• not independent
![Page 54: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/54.jpg)
Fall 2008 Programming Development Techniques
54
encapsulation
• generally a bad idea to put state in the global environment– pollutes namespace– gives other routines access
• even ones that don't need it
– can get bad interactions• even accidental ones
![Page 55: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/55.jpg)
Fall 2008 Programming Development Techniques
55
disciplined use of side-effects
• moral of the story:• there are many things we can do with side-effects• …that we really should not do.• keep your state private!
– nobody else has to know
![Page 56: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/56.jpg)
Fall 2008 Programming Development Techniques
56
controlling access
• What if I want multiple procedures to access the same state?
• want specificity– give multiple procedures access– but not all procedures
![Page 57: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/57.jpg)
Fall 2008 Programming Development Techniques
57
code and data
• encapsulate data– with associated (limited) code to manipulate– code mediates kinds of access
• familiar…– message-passing associated data and code
![Page 58: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/58.jpg)
Fall 2008 Programming Development Techniques
58
message-passing
; returns a procedure that will act as an accumulator
; or will return its value or reset the value• (define (mp-make-accum value)
(lambda (op)
(cond ((eq? op 'accum)
(lambda (x) (set! value
(+ value x))))
((eq? op 'value) value)
((eq? op 'reset) (set! value 0))
(else (error “unknown op: ” op)))))
![Page 59: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/59.jpg)
Fall 2008 Programming Development Techniques
59
message-passing use
• (define a2 (mp-make-accum 0))• (a2 'value)
0• ((a2 'accum) 1)• (a2 'value)
1
![Page 60: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/60.jpg)
Fall 2008 Programming Development Techniques
60
environment• (define a2 (mp-make-accum 0))
current env
![Page 61: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/61.jpg)
Fall 2008 Programming Development Techniques
61
environment• (define a2 (mp-make-accum 0))• (a2 ‘accum)
current env
![Page 62: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/62.jpg)
Fall 2008 Programming Development Techniques
62
environment• (define a2 (mp-make-accum 0))• (a2 ‘value)
current env
![Page 63: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/63.jpg)
Fall 2008 Programming Development Techniques
63
usage
• (define a2 (mp-make-accum 0))• during: ((a2 ‘accum) 1)
current env
![Page 64: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/64.jpg)
Fall 2008 Programming Development Techniques
64
usage
• (define a2 (mp-make-accum 0))• during: ((a2 ‘accum) 1)
current env
![Page 65: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/65.jpg)
Fall 2008 Programming Development Techniques
65
usage
• (define a2 (mp-make-accum 0))• during: ((a2 ‘accum) 1)
current env
(set! value (+ value x))(set! value (+ 0 1))
(set! value 1)
![Page 66: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/66.jpg)
Fall 2008 Programming Development Techniques
66
usage• after: ((a2 ‘accum) 1)
1
current env
![Page 67: Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.](https://reader035.fdocuments.us/reader035/viewer/2022062516/56649da75503460f94a93158/html5/thumbnails/67.jpg)
Fall 2008 Programming Development Techniques
67
Big Ideas
• procedures point to environment at the place where they're evaluated
• procedures can have local state• allows us to create procedure-specific (object-specific)
state• allows us to encapsulate data
– avoiding conflict– control usage