cs61a_sp08_mt2_sol

# cs61a_sp08_mt2_sol - CS 61A 1 Spring 2008 Midterm 2...

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

CS 61A Spring 2008 Midterm 2 solutions 1. What will Scheme print? > (map list '(1 2 3)) ((1) (2) (3)) ---->XX------>XX------>X/ | | | | | | V V V X/ X/ X/ | | | V V V 1 2 3 MAP calls LIST three times, once for each number in the argument list: (LIST 1), (LIST 2), and (LIST 3). The values returned by these LIST calls make up the elements of the returned list. > (let ((x '(1 2)) (y '(8 9))) (cons x (append y x))) ((1 2) 8 9 1 2) +-------------+ | | --->** | x | | | V | V XX------>XX--+--->XX------>X/ | | | | V V V V 8 9 1 2 ^ ^ | | y-->XX------>X/ This picture shows why start arrows are important! The first pair of the result is the one shown as ** (instead of XX like the other pairs). The CAR of that pair is the value of X, which is the list (1 2) formed by the two rightmost pairs of the diagram. The ** pair is the one pair formed by the call to CONS. The call to APPEND produces the four-element list whose spine is the row of four pairs below the ** pair. The first two pairs are made by APPEND; the last two pairs are the ones shared with the value of X. (Note that the two pairs forming the value of Y are /not/ part of the returned value, so it's okay if you left them out of your diagram.) > (cons (cons 1 2) (append '(18 3) '()))

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

View Full Document
((1 . 2) 18 3) ---->XX------>XX------>X/ | | | | V V | 18 3 V XX--->2 | V 1 The main point here was to see if you could draw a correct box and pointer diagram for a pair that isn't a list, the one that prints as (1 . 2). (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. Some people thought that (1 . 2) was a 3-element list with 1, a period, and 2. 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. OOP nomenclature SICP instance book novel child class book title instantiation variable book ASUC Bookstore instance bookstore inventory instance variable* bookstore SICP is a /particular/ book, so it's an instance of the BOOK class. A novel is a /kind of/ book, so it's a child class. (It might, for example, have an instance variable GENRE, with possible values SF, MYSTERY, ROMANCE, etc., which other kinds of books might not have.) A title is a property of a particular book, so it might be an instance variable or an instantiation variable. But we thought that it would almost certainly be an instantiation variable, because the title is specified when the book comes into existence, rather than being computed later as a result of messages sent to the book. The ASUC Bookstore is a particular bookstore, hence an instance.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 9

cs61a_sp08_mt2_sol - CS 61A 1 Spring 2008 Midterm 2...

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

View Full Document
Ask a homework question - tutors are online