{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_sp01_mt2_sol

CS61A_sp01_mt2_sol - CS 61A Spring 2001 Midterm 2 solutions...

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

View Full Document Right Arrow Icon
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))
Image of page 1

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

View Full Document Right Arrow Icon
--->/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.
Image of page 2
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 Louis's base case as if it said (if (null? self) ...).
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern