{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs61a_fa07_mt2_sol

# cs61a_fa07_mt2_sol - CS 61A 1 Fall 2007 Midterm 2 solutions...

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

CS 61A Fall 2007 Midterm 2 solutions 1. What will Scheme print? > (append (cons (list 1 2) (list 2 3)) '(5 6)) ((1 2) 2 3 5 6) ---->XX------>XX------>XX------>XX------>X/ | | | | | | V V V V | 2 3 5 6 V XX------>X/ | | V V 1 2 CONS makes one new pair, but from a list point of view, what it does is take a list as its /second/ argument, and extend that list at the left with one new element, the /first/ argument, whether that's an atom or a list. So (LIST 1 2) returns (1 2), and that becomes the first element of an extension of (2 3). So the CONS returns ((1 2) 2 3) APPEND takes the three elements of that list, and combines them with the two elements of (5 6), producing the five-element list above. By the way, the number 2 appears twice in this list. It doesn't matter if you have two 2s, as above, or one 2 that both pairs point to. > (let ((y (list '(1) 2 3))) (cons '(7 . 8) (cdr y))) ((7 . 8) 2 3) ---->XX------>XX------>X/ | | | | V V | 2 3 V XX / \ V V 7 8 The main point here was to see if you could draw a correct box and

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

View Full Document
pointer diagram for a pair that isn't a list, the one that prints as (7 . 8). (By the way, is this result a list? Should LIST? of it return true? Yes, because the definition of a list only mentions the CDRs of the spine pairs: a list is either the empty list or /a pair whose CDR is a list/. That's true of this structure, even though the /CAR/ of one of the pairs isn't a list. Many people thought that (7 . 8) was a 3-element list with 7, a period, and 8. The dot in "dotted pair" notation is part of the notation, just as the parentheses are part of the notation, not part of the actual data. Scoring: One point per printed form, one point per diagram. At most one point lost for quotation marks anywhere in the answer. 2. Another box and pointer diagram. (3 (9 (2 7) 8) ()) - ----------- -- ---->XX------>XX------>// | | V | 3 | V XX-------->XX-------->X/ | | | V | V 9 | 8 V XX---->X/ | | V V 2 7 This is a three-element list (the elements are underlined in the print form shown above the diagram). So its spine is three pairs. The only slightly tricky part is the third element, which is the empty list. This is shown with a slash through the CAR of the pair, just as an empty list in the CDR of a pair is shown with a slash. Many people made the CAR point to another pair (e.g., of empty lists)! Some people had the CDR of the second pair in the spine point to the empty
list; that would make it a two-element list. 3. Abstract data types. (a) This part should have been easy; a hider is a list of two elements, and the three selectors have to return the first element, the CAR of the second element, and the CDR of the second element: (define (hider-description hider) (CAR HIDER)) ; the first element (define (encoder hider) (CAADR HIDER)) ; the CAR of the second element (CADR) (define (decoder hider) (CDADR HIDER)) ; the CDR of the second element Many people used CADR and CDDR for encoder and decoder, respectively. This would be correct if the the LIST in the constructor had been a CONS instead, but to select the second element of a list, use CADR.

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.

{[ snackBarMessage ]}