f-3 sol - CS 61A Solutions to sample final exam #3 1....

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 61A Solutions to sample final exam #3 1. Domain and range The domain of a procedure is the TYPE of thing(s) it accepts as argument(s). The range is the TYPE of thing it returns. So we wanted types as the answers to this question. That's generally what you gave for the domain, but for the range, several people instead gave explanations of the purpose of the procedure, e.g., "Range: the children of the tree node." (a) CHILDREN CHILDREN takes one argument, a Tree (which is the same as a node). It returns a forest, which is a list of Trees. So: Domain: TREE or NODE Range: FOREST or LIST OF TREES or just LIST Most people got this one. (b) OWNER OWNER takes one argument, a THING object. It returns the *name of* the person who owns the thing (not the person, not the name of the thing). A name is a word, not an object! So: Domain: OBJECT or THING OBJECT or THING INSTANCE Range: WORD We also accepted NAME for the range, although that isn't really a type. The most common wrong answer for the range was PERSON; that's a kind of object, not a word. A fairly common wrong answer for the domain was CLASS; the procedure takes a particular thing as its argument, not the class of all things. Another common wrong answer was PROCEDURE; it's true that we use procedures to represent objects, but not every procedure is an object, and not every procedure can be used as the argument to OWNER. (c) SET-CDR! SET-CDR! takes two arguments; the first must be a pair, and the second can be of any type. Its return value is unspecified, since it is used for its effect rather than for its return value, so we accepted anything for the range in this part. Domain: PAIR and ANYTHING (you had to say both). Range: UNSPECIFIED (but we accepted anything). We didn't accept LIST for the domain; there is no requirement that the first argument to SET-CDR! be a pair whose cdr is a list (which is what it means for a pair to be a list). Also, the LIST domain would include the empty list, which can't be used as the first argument to SET-CDR!. Scoring: One point each, all or nothing. 2. Data abstraction The argument is a LIST of RATIONALs. For the list itself, the appropriate selectors are CAR and CDR. (Constructors for lists include CONS, LIST, and APPEND, but in this problem we are not constructing a list!) For a rational number, the constructor is MAKE-RAT and the selectors are NUMER and DENOM. So the corrected version is (define (ratprod lst) (define (helper lst n d) (if (null? lst) (MAKE-RAT n d) (helper (CDR lst) (* n (NUMER (CAR lst)) (* d (DENOM (CAR lst))))))) (helper lst 1 1)) The call to CDR doesn't change because it is selecting part of a list, not part of a single rational number. The calls to CAAR and CDAR do change, as shown above; they both select the first element of the list (CAR), and each then selects one piece of a rational number (NUMER or DENOM)....
View Full Document

This note was uploaded on 01/15/2011 for the course CS 61A taught by Professor Harvey during the Spring '08 term at University of California, Berkeley.

Page1 / 20

f-3 sol - CS 61A Solutions to sample final exam #3 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