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

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

View Full Document Right Arrow Icon
CS 61A Fall 2009 Midterm 1 solutions 1. What will Scheme print? (every (lambda (x) (se x x)) (keep (lambda (x) (even? (count x))) '(and your bird can sing))) Answer: (your your bird bird sing sing) The function used as argument to KEEP selects words with an even number of letters, and so the KEEP returns (your bird sing). Then EVERY repeats each word twice. Because EVERY uses SENTENCE as its combiner, the results are flattened into a sentence. If we'd used MAP instead of EVERY, the result would have been ((your your) (bird bird) (sing sing)). ((lambda (x y) (x (y 3))) (lambda (x) (* x x)) (lambda (x) (+ x 6))) Answer: 81 By substitution, this is equivalent to ((lambda (x) (* x x)) ((lambda (x) (+ x 6)) 3)) -------------------- -------------------- subst for X subst for Y The inner procedure call ((lambda (x) (+ x 6)) 3) returns 9. So the outer procedure call returns (* 9 9) = 81. Scoring: One point each. If you put quotation marks in your answers, you lost only one point on questions 1 and 2 for that even if you did it more than once. 2. Box and pointer diagrams. (list (list (cons 3 (list 4)))) Answer: (((3 4))) ---->X/ | V X/ | V XX---->X/ | | V V 3 4
Background image of page 1

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

View Full Document Right Arrow Icon
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.
Background image of page 2
Image of page 3
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 Right Arrow Icon
Ask a homework question - tutors are online