cs61a_fa09_mt1_sol

# cs61a_fa09_mt1_sol - CS 61A 1 Fall 2009 Midterm 1 solutions...

This preview shows pages 1–3. Sign up to view the full content.

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

View Full Document
The two troublesome things here are understanding how to represent a one-element list (namely, as a single pair whose CAR is the one element and whose CDR is the empty list), and understanding that CONS just makes one pair, so it sticks the new element 3 at the front of the list (4), giving (3 4). So (LIST (CONS . ..)) gives the one-element list ((3 4)), and (LIST (LIST (CONS . ..))) gives the one-element list (((3 4))). A tempting wrong answer is (((3 . (4)))), thinking that anything created by CONS will print with a dot in the middle. But Scheme doesn't remember how a pair is created, and it never prints a dot followed by an open parenthesis. (append (list 1 2) (list 4 (cons 2 3))) Answer: (1 2 4 (2 . 3)) ---->**----------->**---------->XX----------->X/ | | | | V V V V 1 2 4 XX--->3 | V 2 The call to CONS makes the bottom pair (2 . 3); the call to LIST makes the two-element list (4 (2 . 3)). Dotted pairs can be elements of lists! (And that doesn't make the overall list improper.) The pairs marked as ** instead of XX are /copies of/ the spine of the list created by (LIST 1 2), since APPEND copies the spines of all but its last argument. In this case that doesn't matter, because we don't use the original list for anything after the APPEND is done. Scoring: One point per print form, one point per box and pointer diagram.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

cs61a_fa09_mt1_sol - CS 61A 1 Fall 2009 Midterm 1 solutions...

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

View Full Document
Ask a homework question - tutors are online