CS61A_sp01_mt2_sol

CS61A_sp01_mt2_sol - CS 61A Spring 2001 Midterm 2 solutions 1 Box and pointer > c lets(go bears->->XX->XX->X | | | V V V lets X bears | V go The

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

CS 61A Spring 2001 Midterm 2 solutions 1. Box and pointer. > c (() lets (go) bears) --->/*--->XX----->XX---->X/ | | | V V V lets X/ bears | V go The pair marked as /* is the one created by the CONS. Its *CAR* is slashed because it's null (the empty list is an element of the main list). The most common mistake was to represent the empty list with an extra pair: --->XX--->XX----->XX---->X/ | | | | | V V V | lets X/ bears | | | V | go | V // <---- This is wrong! A pair with both car and cdr slashed represents (()), a one-element list containing the empty list. We accepted an arrow pointing to () [but not to '() -- there aren't any quotes in values, only in expressions you type] and we accepted an arrow pointing to a *single* slashed square. But a slash right in the pair, as shown above, is the correct notation. > a (lets (go) bears) --->XX----->XX---->X/ | | | V V V lets X/ bears | V go Appending an empty list to something doesn't change its value. (APPEND combines the *elements* of its arguments, and the empty list has no elements to contribute to the result.) > l (() (lets (go) bears))

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

View Full Document
--->/X--->X/ | V XX----->XX---->X/ | | | V V V lets X/ bears | V go LIST always creates a list with as many elements as it has arguments -- two, in this example. The first element is the empty list; the second is that other list we keep seeing in this question! > (cdr c) (lets (go) bears) > (cdr a) ((go) bears) > (cdr l) ((lets (go) bears)) In each case the way to think about the problem is to look at the first pair in the box and pointer diagram and take the cdr of that pair. Scoring: One point per printed representation, one point per box and pointer diagram. No part credit! Exception: consistently putting quotation marks in the results loses at most 3 points. Another exception: If the printed form in the first set of answers is wrong, but the corresponding box and pointer diagram agrees with that wrong answer, and so does the printed form of the cdr, the score is one point (instead of zero) for those three answers. 2. OOP lists. The point of this question was that an object-oriented solution has to be consistently object-oriented. That means that everything is an object, and also that there shouldn't be type-dispatching code within a method; if you have something like (if (type-1? arg) (do-type-1-algorithm arg) (do-type-2-algorithm arg)) anywhere, then type-1 and type-2 should usually be separate classes instead, perhaps inheriting from a common parent (although not in this case). (a) What does Louis's implementation do? > (ask my-list 'length) 1 > (ask other-list 'length) ERROR -- attempt to apply non-procedure (). Louis's implementation works for non-empty lists, but his empty list isn't an object, so you can't send it messages. This is the design error that Dan Ingalls described when he complained about languages that aren't 100% object-oriented.
The common wrong answers for this part were 1 and 0, which is what a correct LENGTH implementation would do, and 1 and 1, presumably because people read

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.

This note was uploaded on 11/23/2010 for the course CS 61A taught by Professor Harvey during the Fall '08 term at University of California, Berkeley.

Page1 / 9

CS61A_sp01_mt2_sol - CS 61A Spring 2001 Midterm 2 solutions 1 Box and pointer > c lets(go bears->->XX->XX->X | | | V V V lets X bears | V go The

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

View Full Document
Ask a homework question - tutors are online