cs61a_sp09_mt2_sol - 61A 1. Spring 2009 Box and pointer...

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

View Full Document Right Arrow Icon
61A Spring 2009 Midterm 2 solutions 1. Box and pointer diagram. Before the APPEND we have this: x--->XX---->X/ y--->XX---->X/ | | | | V V V V 2 3 4 5 After the APPEND we have this: z--->XX---->XX--------------+ | | | V V | 2 3 | V x--->XX---->X/ y--->XX---->X/ | | | | V V V V 2 3 4 5 because APPEND copies the spine pairs of all but its last argument. So in this case the result of the APPEND shares spine pairs with list Y, but not with list X. Then after the CONS we have this: +-------+ | | | V | z--->**---->**--------------+ | | | | | V V +------\ | | 2 3 | \| | | V | x--->XX---->X/ | y--->**---->*/ | | | | | | | V V | V V +----+ 2 3 | 4 5 | | | | ---->** | | | +-----------------+ The start arrow is in the bottom left corner of this diagram. The pairs marked as ** and */ form the spine of the resulting list; you can tell this by following the CDRs from one pair to another, starting with the
Background image of page 1

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

View Full DocumentRight Arrow Icon
start pair, the one made by the CONS. You can see from the diagram that this is a list of five elements: ((4 5) 2 3 4 5) Because we're not yet at the point in the course at which it really matters whether or not the two occurrences of the list (4 5) in the result, first as an element and then as a sublist, are the same pairs, we also accepted diagrams like this: ---->XX----------->XX---->XX---->XX---->X/ | | | | | V V V V V XX---->X/ 2 3 4 5 | | V V 4 5 But you should be aware that on midterm 3 there will probably be a similar question, and that time you have to get the distinction between equality and identity (to be discussed next week) right! Scoring: 2 points for the diagram, 2 points for the print form. 2. OOP categories. (a) A chimney is /part of/ a house, not a kind of house, so this is an INSTANCE VARIABLE. (b) DRIVE is something you can /do with/ an automobile, so it's a METHOD. (c) A comic book is a /kind of/ book, so it's a CHILD CLASS. (d) Although you might use binoculars to look at animals in the jungle, binoculars are neither part of an animal nor a kind of animal, so it's NONE OF THE ABOVE. (e) A keyboard is /part of/ a computer, so it's an INSTANCE VARIABLE. This should have been easy, if you understand how objects fit together. Scoring: One point each. 3. Scheme-1. You are asked to make TWICE a /primitive procedure/ in Scheme-1. The primitives of Scheme-1 are STk procedures, not built into EVAL-1 or APPLY-1. So you're going to define an STk procedure:
Background image of page 2
(define (twice f) (lambda (x) (apply-1 f (list (apply-1 f (list x)))))) The two calls to LIST are there because APPLY-1's second argument has to be a /list of argument values/; in this case it's a list of length 1, because F takes one argument. More importantly, the calls to APPLY-1 are there because F can be any Scheme-1 procedure, which might or might not be an STk procedure! If you just say (LAMBDA (X) (F (F X))) then F has to be an STk procedure, i.e., a Scheme-1 primitive. So this won't work: Scheme-1> ((twice (lambda (x) (* x x))) 5) You were asked to make TWICE a procedure, not a special form. So a
Background image of page 3

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

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

Page1 / 10

cs61a_sp09_mt2_sol - 61A 1. Spring 2009 Box and pointer...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online