CS61A_fa94_f_sol

CS61A_fa94_f_sol - CS 61A Fall 1994 Final exam solutions 1...

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

View Full Document Right Arrow Icon
CS 61A Fall 1994 Final exam solutions 1. Higher-order functions. (define (multicompose funs) (lambda (x) (if (null? funs) x ((car funs) ((multicompose (cdr funs)) x))))) Two crucial points to note: The return value in the base case is *not* the empty list; the parentheses in the last line are very important! Many other arrangements of the same idea are possible. If those parentheses seem frightening, some of them can be avoided by using a helper function: (define (multicompose funs) (define (helper funs x) (if (null? funs) x ((car funs) (helper (cdr funs) x)))) (lambda (x) (helper funs x))) An even more elegant solution uses the COMPOSE function that you've seen before: (define (compose f g) (lambda (x) (f (g x)))) (define (multicompose funs) (if (null? funs) (lambda (x) x) (compose (car funs) (multicompose (cdr funs))))) Typical errors: 4 points Use LAST, BUTLAST (a data abstraction violation because the argument is a list of procedures, not a sentence which is a list of words). Bad base case (most often (lambda (x) '())). 3 points Apply the functions in reverse order. Try to avoid the reverse-order bug by reversing the list, but fail. Forget to invoke the result of the recursive call. No base case at all. 2 points Apply each function separately to x instead of composing them. Call multicompose with two arguments (as if it were helper). Name/value confusion, such as thinking LIST is in the argument list, in the examples, or calling EVAL on an element of the argument list. 1 point Don't return a function (for example, return a list of functions).
Background image of page 1

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

View Full DocumentRight Arrow Icon
0 points Use SET!. (The question did say, in boldface, "no mutation!") 2. Abstract data type (a) Add two prices, respecting the abstraction. (define (+price a b) (let ((pence (+ (cents a) (cents b)))) (if (< pence 100) (make-price (+ (dollars a) (dollars b)) pence) (make-price (+ (dollars a) (dollars b) 1) (- pence 100))))) Scoring: 3 points if correct; 2 points for not noticing if the number of cents is >= 100; 1 point for (+ a b); 0 if even worse. Note that, since a price is represented internally as a number of cents, the procedures listed as 2-point and 1-point solutions would work correctly. But, as we'll see in part (b), they would stop working if the representation is changed. (b) Change to list representation (define make-price list) (define dollars car) (define cents cadr) Scoring: 2 points if correct; 1 point for a change to some other consistent representation that wasn't what we asked for (example: cons, car, cdr); 0 points for an inconsistent representation or anything requiring a change to +price. 3.
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.

Page1 / 7

CS61A_fa94_f_sol - CS 61A Fall 1994 Final exam solutions 1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online