{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs61a_final_sp01_sol - CS 61A 1 Spring 2001 Final exam...

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

View Full Document Right Arrow Icon
CS 61A Spring 2001 Final exam solutions 1. Simple recursion and higher order functions (a) There are two ways to do this: chopping off characters from the left, or gathering characters from the right. (define (last-name name) ; left-to-right (if (eq? (first name) '-) (butfirst name) (last-name (butfirst name)))) (define (last-name name) ; right-to-left (if (eq? (last name) '-) "" (word (last-name (butlast name)) (last name)))) Scoring: 2 points, all or nothing. (Really trivial errors ignored.) We accepted FIRST and REST instead of FIRST and BUTFIRST, but no points for using CAR and CDR, which don't work on words! (b) What type of process: The left-to-right solution to part (a) generates an ITERATIVE process, because the recursive call to LAST-NAME isn't the argument to another procedure call. (IF is a special form, and evaluating the recursive call is the last thing it has to do.) The right-to-left solution generates a RECURSIVE process, because the recursive call to LAST-NAME is used as an argument to the call to WORD, so there's still more work to be done when the recursive call returns. Scoring: 2 points, all or nothing. (c) higher-order filtering function: (define (make-filterer good-child?) (lambda (names) (filter good-child? names))) We also accepted KEEP and KEEP-IF as alternate names for FILTER (coming from various versions of CS 3). It's possible to do it in more complicated ways, using MAP to reinvent FILTER. We accepted the ones that worked, but you ultimately have to remove the unwanted names, not replace them with #F or () or "". GOOD-CHILD? is the formal parameter to MAKE-FILTERER.
Background image of page 1

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

View Full Document Right Arrow Icon
GOOD-KIDS? is the name of an example, and should not be part of your solution. It's especially bad if both GOOD-CHILD? and GOOD-KIDS? are used in your procedure. Scoring: 2 points, all or nothing. No points for recursive procedures! 2. Big ideas. NASA didn't know the data type of the number they were given. It would have helped if the number had its type attached: (POUNDS . 100) or (KILOGRAMS . 100) as the case may be. This idea is called TYPE-TAGGED DATA. None of the others were at all relevant; this wasn't a "find the best answer" sort of problem. Scoring: One point, all or nothing! 3. Dynamic vs. lexical scope Many students found this a difficult problem. In the first example, the situation is this: The LET expression is equivalent to a lambda and an invocation: ( (lambda (f x) (f 25)) (lambda (y) (+ x y)) 100) ^ ^ | | the LET procedure its args Neither LAMBDA expression is internal to another one; both are typed by the user directly at the Scheme prompt (in the form of a LET). The use of LET might make it seem otherwise, because the (lambda (y) ...) is inside the LET. But it's not inside the *body* of the LET! The expressions for the values of the LET variables are (as you can see by looking at the un-abbreviated version above) evaluated *outside* the scope of the LET.
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.

{[ snackBarMessage ]}