{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_fa98_mt2_sol

CS61A_fa98_mt2_sol - CS 61A Fall 1998 Midterm 2 solutions 1...

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

View Full Document Right Arrow Icon
CS 61A Fall 1998 Midterm 2 solutions 1. Box & pointer diagrams > (append (list 'a 'b) '(c d)) (A B C D) --->XX----->XX----->XX----->X/ | | | | V V V V A B C D Most people got this. The most common problem was to put quotation marks in the answer, in the printed result and/or in the diagram. The quotation marks are part of Scheme expressions, but not part of the value of those expressions! In other words, the value of the expression 'A is just A, not 'A. > (cons (list 'a 'b) (cons 'c 'd)) ((A B) C . D) --->XX------------->XX--->D | | V V XX----->X/ C | | V V A B The pair at the top, to which the initial arrow points, is the one created by the CONS. This was the part most commonly answered incorrectly. The least bad wrong answer was ((A B) . (C . D)). This is close to correct, in that if you quoted it to Scheme, you'd get this data structure. But Scheme never prints a dot followed by an open parenthesis! It uses list notation instead. (In this case it's an "improper list" because it doesn't end with an empty list in the last cdr.) Other wrong answers were ((A B) C D), which would be a proper list with an extra pair, as if the problem had been (cons (list 'a 'b) (LIST 'c 'd)) and (A B C . D), a structure of only three pairs, as if the problem had been (APPEND (list 'a 'b) (cons 'c 'd)) > (list (list 'a 'b) (append '(c) '(d))) ((A B) (C D)) --->XX------------->X/ | | V V XX----->X/ XX----->X/ | | | | V V V V A B C D The two pairs in the top row are the ones generated by LIST. It
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
always creates a list whose length is the number of arguments, which is two in this example. The first element is (A B); the second is (C D). > (cdar '((1 2) (3 4))) (2) --->X/ | V 2 (car '((1 2) (3 4))) --> (1 2) (cdr '(1 2)) --> (2) Scoring: 1/2 point per printed form, 1/2 point per box and pointer diagram, rounded down to the nearest point. Exception: Side-issue mistakes made consistently in all problems, such as putting quotation marks in the result, lost no more than 1 point altogether in the printed form and 1 point altogether in the diagram. Note: When drawing box and pointer diagrams, please be careful about the arrows; it makes your diagrams much easier to read. There should be an arrowhead at the end (next to the thing the arrow points at). The tail of the arrow should be INSIDE a box; the head should be NEXT TO a box or other datum. For example: +-----+-----+ | | /| ----->| | | / | | | |/ | +--|--+-----+ | V 2 Note how the arrow that points to the 2 starts inside the car of the pair. 2. Using data abstraction. (a) assoc (define (assoc key a-list) (cond ((NULL? a-list) #f) ((equal? key (ASSOCIATION-KEY (CAR a-list))) (CAR a-list)) (else (assoc key (CDR a-list))))) (b) index and index-one (define (index groups) (if (NULL? groups) '() (append (index-one (CAR groups)) (index (CDR groups))))) (define (index-one group)
Image of page 2
Image of page 3
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