Chapter 3, Modularity, Objects, and State

Exercise 3.12


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#lang sicp

(define (append! x y)
  (set-cdr! (last-pair x) y)
  x)


(define (last-pair x)
  (if (null? (cdr x))
      x
      (last-pair (cdr x))))


(define x (list 'a 'b))
(define y (list 'c 'd))
(define z (append x y))

Output:

1
2
3
4
5
6
7
8
9
10
11
12
> (display z)
(a b c d)
> (display (cdr x))
(b)
> (define w (append! x y))
> (display w)
(a b c d)
> (display x)
(a b c d)
> (display (cdr x))
(b c d)
>