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.
BESTTOTAL takes a hand (a sentence) as argument and returns a number.
No procedures; not higher order.
STOPAT17 takes a hand (a sentence) and a card (a word) as arguments,
returning #T or #F.
No procedures; not higher order.
PLAYN takes a strategy (which is a procedure!) and a number as arguments,
returning a score.
It's higher order.
STOPAT 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 STOPAT).
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 onesentence 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 DocumentMany 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
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Harvey
 Computer Programming, lambda expression

Click to edit the document details