Title: CS 3015 Lecture 16
1CS 3015 Lecture 16
2Evaluating set!
- Evaluating the expression
- (set! ltvariablegt ltvaluegt)
- Locates the binding of the variable in the
environment and changes that binding to the new
value
3Return to an Early Example
- (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)))
4Example contd
5Evaluating (f 5)
6Comments
- Observe that each call to square creates a new
environment containing a binding for x - different frames serve to keep separate the
different local variables all named x - Each frame created by square points to the global
environment, since this is the environment
indicated by the square procedure object.
7Frames as a Repository for Local State
- Frames are used to create instance variables
- (define (make-withdraw balance)(lambda (amount)
(if (gt balance amount) (begin - (set! balance (- balance amount)) bal
ance) "Insufficient funds")))
8The Withdraw Example
- Let us describe the evaluation of
- (define W1 (make-withdraw 100))
- followed by
- (W1 50)50
9Result of Defining Make-withdraw
10Evaluating (define W1 (make-withdraw 100))
11Evaluating (W1 50)
12Evaluating (define W2 (make-withdraw 100))
13Internal Definitions
- (define (sqrt x)(define (good-enough? guess) (lt
(abs (- (square guess) x)) - 0.001))(define (improve guess) (average
guess (/ x guess)))(define (sqrt-iter guess) (i
f (good-enough? guess) guess (sqrt-ite
r (improve guess))))(sqrt-iter 1.0))
14Why Internal Definitions Behave as Desired
15Comments
- The names of the local procedures do not
interfere with external names, because the local
names are bound in the frame created when the
procedure is run - The local procedures can access the arguments of
the enclosing procedure, simply by using
parameter names as free variables. - The local procedure is evaluated in an
environment that is subordinate to the evaluation
environment for the enclosing procedure.
16Example Environment Model
- (define (product f a next b)
- (if (gt a b) 1
- ( (f a)
- (product f (next a) next b))))
- (define (fact n)
- (product (lambda (x) x) 1
- (lambda (x) ( x 1)) n))
17Global Env
Product fact
Arguments n Code
Arguments f,a,next,b Code
18Global Env
Product fact
(fact 3)
Arguments n Code
Arguments f,a,next,b Code
E1
n 3
Call to fact
f
a 1 next b 3
Arguments x Code x
Arguments x Code x1
First call to product
19Global Env
Product fact
(fact 3)
Arguments n Code
Arguments f,a,next,b Code
E1
n 3
Call to fact
f
E3
a 1 next b 3
f
a 2 next b 3
Arguments x Code x
Arguments x Code x1
E2
First call to product
Second call to product
20Modeling with Mutable Data
- We now need to return to our design methodology
and see how it should be changed given mutable
data - In addition to selectors and constructors, we
will now have mutators - (set-balance ltaccountgt ltnew-valuegt)
- We start with mutators for the built in data
object cons cells - Up to now, we have not been able to modify lists
21Mutable List Structure
- Introduce two new operations
- set-car! and set-cdr!
- (set-car! x y)
22Effect of set-cdr!
- (define z (cons y (cdr x)))
23(set-cdr! x y)
24Could Implement cons as
- (define (cons x y)
- (let ((new (get-new-pair)))
- (set-car! new x)
- (set-cdr! new y)
- new))
25Sharing and Identity
- (define x (list 'a 'b))(define z1 (cons x x))
26In contrast to
- The list z2 formed by
- (cons (list 'a 'b) (list 'a 'b))
27Consider the Difference
- (define (set-to-wow! x) (set-car! (car x) 'wow)
x) - Z1
- ((a b) a b)
- (set-to-wow! z1)
- ((wow b) wow b)
- Z2
- ((a b) a b)
- (set-to-wow! z2)
- ((wow b) a b)
28Generalization of eq?
- In addition to testing for same symbol, eq? tests
whether or not two pointers are the same - (eq? (car Z1) (cdr Z1))
- true
- (eq? (car Z2) (cdr Z2))
- ()
29Mutation is Just Assignment
- (define (cons x y) (define (set-x! v) (set! x v)
) (define (set-y! v) (set! y v)) (define (disp
atch m) (cond ((eq? m 'car) x) ((eq?
m 'cdr) y) ((eq? m 'set-car!) set-x!)
((eq? m 'set-cdr!) set-y!) (els
e (error " m)))) dispatch)
30- (define (car z) (z 'car))
- (define (cdr z) (z 'cdr))
- (define (set-car! z new-value) ((z 'set-car!) ne
w-value) z) - (define (set-cdr! z new-value) ((z 'set-cdr!) ne
w-value) z)