CS 61A         Summer 2011                Lab 9

1. Exercise 3.12 of Abelson and Sussman.

2. Suppose that the following definitions have been provided.

(define x (cons 1 3))

(define y 2)

 

A CS 61A student, intending to change the value of x to a pair with car equal to 1 and cdr equal to 2, types the expression (set! (cdr x) y) instead of (set-cdr! x y) and gets an error. Explain why.

 

3a. Provide the arguments for the two set-cdr! operations in the blanks below to produce the indicated effect on list1 and list2. Do not create any new pairs; just rearrange the pointers to the existing ones.

> (define list1 (list (list ’a) ’b))

list1

> (define list2 (list (list ’x) ’y))

list2

> (set-cdr!                     )

okay

> (set-cdr!                     )

okay

> list1

((a x b) b)

> list2

((x b) y)

 

3b.  After filling in the blanks in the code above and producing the specified effect on list1 and list2, draw a box-and-pointer diagram  that explains the effect of evaluating the expression (set-car! (cdr list1) (cadr list2)) .

 

4. Exercises 3.13 and 3.14 in Abelson and Sussman.