CS61A_sp94_f_sol

CS61A_sp94_f_sol - CS 61A Spring 1994 Final exam solutions...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 61A Spring 1994 Final exam solutions 1a. (let ((x (cons '() '()))) (set-car! x x) (set-cdr! x x) x) --------- | | | ------->| * | * | /--->| | | | |<---\ | --|---|-- | | | | | | | | | \____/ \____/ 1b. (define (funny! x) (if (null? x) '() (let ((temp (cdr x))) (funny! temp) (set-cdr! x (car x)) (set-car! x temp) x))) (funny! (list 1 2)) On first entry to funny! we have: --------- --------- | | | | | /| ------->| * | *--------->| * | / | | | | | | | |/ | --|------ --|------ | | V V 1 2 Let's call the left pair I and the right pair II. So the first thing we do is bind TEMP to pair II and call funny! again on that pair. The second invocation of funny! binds TEMP to '() and invokes funny! for a third time, with '() as the argument. This third invocation has no effect. Back in the second invocation, X is pair II and TEMP is '(), so after the set-cdr! and the set-car! we have --------- --------- | | | | /| | ------->| * | *--------->| / | * | | | | | |/ | | | --|------ ------|-- | | V V 1 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
That finishes the second funny! so we are back in the first funny! with X bound to pair I and TEMP bound to pair II. So after the set-cdr! and the set-car! we have --------- --------- | | | | /| | ------->| * | * | /-->| / | * | | | | | | | |/ | | | --|---|-- | ------|-- | | | | | V | V | | | 1 | 2 | | \_________/ Basically, what funny! does is to interchange the car and cdr of each pair of the list. A typical one-point error was to swap the parts of only one pair. 1c. We want to generate the structure .-----------------------------. | | V | --------- --------- | | | | | | /| | -------->| | | ----------->| | | / | | | | | | | | |/ | | --|------ --|------ | | ^ | | | | | | | | | | V | V | ------|-- --------- | | | | | | | | | | | | | | | | | --------' | | | | | | | | --|------ --|------ | | | | | | V V A B Call the pairs in the top row I and II, and the ones in the bottom row III and IV. One straightforward way to generate any such structure is to create four isolated pairs and make all the links by hand. Many people did this on the exam: (let ((I (cons '() '())) (II (cons '() '())) (III (cons '() '()))
Background image of page 2
(IV (cons '() '()))) (set-car! I III) (set-cdr! I II) (set-car! II IV) ; we could say (set-cdr! II '()) but it's not needed (set-car! III 'A) (set-cdr! III I) (set-car! IV 'B) (set-cdr! IV I) I) This is perfectly fine. You can do it more easily if you notice that most of the structure is perfectly ordinary, a list of two singleton lists: (let ((x (list (list 'A) (list 'B)))) (set-cdr! (car x) x) (set-cdr! (cadr x) x) x) Note that it's not permissible to say (let ((x '((A) (B)))) . ..) because you aren't allowed to mutate constant (quoted) lists, but we didn't take off points for that. Other shortcut solutions are possible, such as this one: (let ((x (list '() '()))) (set-car! x (cons 'A x)) (set-car! (cdr x) (cons 'B x)) x) A very common one-point error was to think that the arrow from the cdr of the lower-left pair points to the CDR of the upper-left pair, rather than to the whole pair, like this:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

CS61A_sp94_f_sol - CS 61A Spring 1994 Final exam solutions...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online