f-2 sol - CS 61A 1. Solutions to sample final exam #2 HOFs...

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

View Full Document Right Arrow Icon
CS 61A Solutions to sample final exam #2 1. HOFs in 21 project A higher order procedure is one that uses procedures as data -- either as arguments or as its return value. Many people forgot about the latter. BEST-TOTAL takes a hand (a sentence) as argument and returns a number. No procedures; not higher order. STOP-AT-17 takes a hand (a sentence) and a card (a word) as arguments, returning #T or #F. No procedures; not higher order. PLAY-N takes a strategy (which is a procedure!) and a number as arguments, returning a score. It's higher order. STOP-AT takes a number as argument, returning a strategy -- a procedure. So it's higher order. MAJORITY takes three strategies as arguments and returns a strategy. Clearly higher order! Scoring: 2 points if correct; 1 point if all but one correct (usually leaving out STOP-AT). 2. Foo and baz. (foo 3) ==> (if 3 (foo #f) 5) ==> (foo #f) ==> (if #f (foo #f) 5) ==> 5 (baz 3) ==> (and 3 (baz #f) 5) ==> (and (baz #f) 5) ; since 3 is true ==> (and (and #f (baz #f) 5) 5) ==> (and #f 5) ; inner AND is false ==> #f Scoring: 1 point each. 3. Iterative and recursive. FOO is iterative; BAZ is recursive. In FOO, when IF decides to evaluate (foo #f), it already knows that whatever the answer from (foo #f) is will be the answer to the entire problem. In BAZ, after the (baz #f) returns, AND must check whether the answer is true or false. If false (as, in fact, it is), then indeed the answer to (baz #f) is also the answer to (baz 3). But AND didn't know that until the recursive call is complete! If (baz #f) had been true, the answer would have been 5. A good one-sentence answer: "An iterative process is one in which
Background image of page 1

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

View Full DocumentRight Arrow Icon
the caller has no more work to do once the recursive callee returns." Many people quoted sentences from the book, often the one about "deferred operations"; we accepted these if it sounded as if you might have some idea what it actually meant. Some people quoted a sentence about how something "depends on the return value" but mostly these answers were wrong: the answer to the overall problem does depend, in both cases, on the answer to the recursive call. The question is whether it also depends on anything else! Worst of all was "FOO is iterative because IF is a special form." Sorry -- AND is a special form, too! Scoring: 2 points if you had FOO iterative and BAZ recursive, with a convincing sentence. 1 point if you had FOO iterative and BAZ recursive. (If you had FOO recursive and/or BAZ iterative, we didn't even read your sentence.) 4. How are the pairs connected? We know that A's box and pointer diagram looks like this: A --> [1 . -]---> [2 . -]---> [ | . -]---> [6 . /] | V [3 . -]---> [4 . -]---> [5 . /] We also know that (CDDR B) is the same pair as (CADDR A), which is the pair whose car is 3. Therefore the list (3 4 5) is shared between A and B, so the diagram is A --> [1 . -]---> [2 . -]---> [ | . -]---> [6 . /] | V B --> [1 . -]---> [2 . -]---> [3 . -]---> [4 . -]---> [5 . /] We also know that (CADDR C) is *not* the same pair as (CADDR A), so the list
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.

This note was uploaded on 01/15/2011 for the course CS 61A taught by Professor Harvey during the Spring '08 term at University of California, Berkeley.

Page1 / 10

f-2 sol - CS 61A 1. Solutions to sample final exam #2 HOFs...

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