Chapter 3, Modularity, Objects, and State

Exercise 3.16


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
> (define p1 (cons 'a '()))
> (define p2 (cons 'b '()))
> (define p3 (cons 'c '()))
> (set-cdr! p2 p3)
> (set-cdr! p1 p2)
> (count-pairs p1)
3
> (display p1)
(a b c)
> (set-car! p1 p2)
> (display p1)
((b c) b c)
> (count-pairs p1)
5
> (set-car! p1 p3)
> (display p1)
((c) b c)
> (count-pairs p1)
4
> (set-car! p2 p3)
> (display p2)
((c) c)
> (set-car! p1 p2)
> (display p1)
(((c) c) (c) c)
> (count-pairs p1)
7
> (set-car! p1 p1)
> (count-pairs p1)

; The above call did not return