{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

f-2 sol

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

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

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern