Blob


1 (define (cons x y)
2 (define (set-x! v) (set! x v))
3 (define (set-y! v) (set! y v))
4 (define (dispatch m)
5 (cond ((eq? m 'car) x)
6 ((eq? m 'cdr) y)
7 ((eq? m 'set-car!) set-x!)
8 ((eq? m 'set-cdr!) set-y!)
9 (else (error "Undefined operation -- CONS" m))))
10 dispatch)
11 (define (car z) (z 'car))
12 (define (cdr z) (z 'cdr))
13 (define (set-car! z new-value)
14 ((z 'set-car!) new-value)
15 z)
16 (define (set-cdr! z new-value)
17 ((z 'set-cdr!) new-value)
18 z)
20 Assignment is all that is needed, theoretically, to account for the behavior of mutable data. As soon as we admit set! to our language, we raise all the issues, not only of assignment, but of mutable data in general.21
22 Exercise 3.20. Draw environment diagrams to illustrate the evaluation of the sequence of expressions
24 (define x (cons 1 2))
25 (define z (cons x x))
26 (set-car! (cdr z) 17)
27 (car x)
28 17