{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs61a sp09 midterm1

cs61a sp09 midterm1 - CS 61A 1 Spring 2009 Midterm 1...

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

View Full Document Right Arrow Icon
CS 61A Spring 2009 Midterm 1 solutions 1. What will Scheme print? (every (lambda (x) (se x x)) (map (lambda (x) (* x x)) '(1 2 3))) Answer: (1 1 4 4 9 9) The result of the MAP call is (1 4 9), and the EVERY call makes a sentence in which every number appears twice. The main point of this problem is that if we'd used MAP instead of EVERY, the answer would have been ((1 1) (4 4) (9 9)), but EVERY uses SENTENCE to combine the individual results, and SENTENCE flattens out its argument sentences. (keep (lambda (x) x) (keep even? '(1 2 3 4 5 6 7))) Answer: (2 4 6) The inner KEEP call keeps even numbers, so it returns (2 4 6). The interesting part of the question is the outer KEEP, because its first argument isn't a conventional predicate function; it's the identity function. But in Scheme everything other than #F counts as true, so KEEP keeps all the numbers in the sentence. (let ((first last) (last first)) (last (first '(for no one)))) Answer: o The main point of this question is that LET doesn't carry out the specified bindings one by one, but rather all at once. So the binding (LAST FIRST) binds the name LAST to /the original, global/ value of FIRST, not to the rebound FIRST set up by the (FIRST LAST) binding. Thus, the body of the LET returns the first letter of the last word of the argument sentence. (cadadr '((a b c d e) (f g h i j) (k l m n o))) Answer: g The quick way to do this problem is to recognize that CADR means "the second element," and CADADR means the CADR of the CADR, so
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
it returns the second word [G] in the second sublist [(F G H I J)] of the argument. If you didn't think of that, you can work out the answer by doing the CARs and CDRs from the inside out, i.e., starting with the /rightmost/ A or D in the name -- in this case, a D, for CDR: (cdr '((a b c d e) (f g h i j) (k l m n o))) ==> ((f g h i j) (k l m n o)) (car '((f g h i j) (k l m n o)) ==> (f g h i j) (cdr '(f g h i j)) ==> (g h i j) (car '(g h i j)) ==> g If you did the CARs and CDRs from left to right, you'd have tried to take the CDR of the word B and gotten an error. 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. (append (cons '(1) '(2)) (list '(1) '(2))) Answer: ((1) 2 (1) (2)) ---->**----------->XX---------->XX----------->X/ | | | | V V V V X/ 2 X/ X/ | | | V V V 1 1 2 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 (1) at the front of the list (2), giving ((1) 2). The pair shown as ** above is /a copy of/ the one made by the CONS call, since APPEND copies the spine of all but its last argument. (list (cons 2 3)) Answer: ((2 . 3))
Background image of page 2
---->X/ | V XX--->3 | V 2 The call to CONS makes the bottom pair (2 . 3); the call to LIST makes a one-element list containing that pair. Improper lists can be elements of lists! (And that doesn't make the overall list improper.) Scoring: One point per print form, one point per box and pointer diagram.
Background image of page 3

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

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

{[ snackBarMessage ]}