{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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 C must have its own distinct pair with 3 in the car. On the other hand, (CDADDR C) *is* the same as (CDDDR B), so the pairs with 4 and 5 in the cars are shared between C and the other lists: A --> [1 . -]---> [2 . -]---> [ | . -]---> [6 . /] | V B --> [1 . -]---> [2 . -]---> [3 . -]---> [4 . -]---> [5 . /] ^ / [3 . -]--/ ^ | C --> [1 . -]---> [2 . -]---> [ | . -]---> [6 . /] (Actually, we don't know whether or not A and C share the pair whose car is 6. But this will turn out not to affect the solution.)
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern