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 ;; Exercise 3.20. Draw environment diagrams to illustrate the evaluation of the sequence of expressions
22 (define x (cons 1 2))
23 (define z (cons x x))
24 (set-car! (cdr z) 17)
25 (car x)
26 17