CS 3015 Lecture 16 - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CS 3015 Lecture 16

Description:

Mutable List Structure. Introduce two new operations. set-car! and set-cdr! (set-car! ... new y) new)) Sharing and Identity (define x (list 'a 'b)) (define z1 ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 31
Provided by: jeffreyv
Category:
Tags: lecture

less

Transcript and Presenter's Notes

Title: CS 3015 Lecture 16


1
CS 3015 Lecture 16
2
Evaluating set!
  • Evaluating the expression
  • (set! ltvariablegt ltvaluegt)
  • Locates the binding of the variable in the
    environment and changes that binding to the new
    value

3
Return 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)))

4
Example contd
5
Evaluating (f 5)
6
Comments
  • 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.

7
Frames 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")))

8
The Withdraw Example
  • Let us describe the evaluation of
  • (define W1 (make-withdraw 100))
  • followed by
  • (W1 50)50

9
Result of Defining Make-withdraw
10
Evaluating (define W1 (make-withdraw 100))
11
Evaluating (W1 50)
12
Evaluating (define W2 (make-withdraw 100))
13
Internal 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))

14
Why Internal Definitions Behave as Desired
15
Comments
  • 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.

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

17
Global Env
Product fact
Arguments n Code
Arguments f,a,next,b Code
18
Global 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
19
Global 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
20
Modeling 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

21
Mutable List Structure
  • Introduce two new operations
  • set-car! and set-cdr!
  • (set-car! x y)

22
Effect of set-cdr!
  • (define z (cons y (cdr x)))

23
(set-cdr! x y)
24
Could Implement cons as
  • (define (cons x y)
  • (let ((new (get-new-pair)))
  • (set-car! new x)
  • (set-cdr! new y)
  • new))

25
Sharing and Identity
  • (define x (list 'a 'b))(define z1 (cons x x))

26
In contrast to
  • The list z2 formed by
  • (cons (list 'a 'b) (list 'a 'b))

27
Consider 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)

28
Generalization 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))
  • ()

29
Mutation 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)
Write a Comment
User Comments (0)
About PowerShow.com