CS61A_fa98_f_sol

CS61A_fa98_f_sol - CS 61A Fall 1998 Final exam solutions 1...

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

CS 61A Fall 1998 Final exam solutions 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 the caller has no more work to do once the recursive callee returns."

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

View Full Document