{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

f-3 sol

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

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

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

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

View Full Document
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).
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