cs61a_sp09_mt3_sol

cs61a_sp09_mt3_sol - CS 61A 1 Midterm 3 solutions Spring...

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

View Full Document Right Arrow Icon
CS 61A Midterm 3 solutions Spring 2009 1. Box and pointer. (define x (list 1 2)) (define y (list 3 4)) (define z (append x y)) (set-car! (cdr x) 5) (set-car! y 6) z Result: (1 2 6 4) 5 6 ^ ^ | | X ----> XX--->X/ Y --+--> XX--->X/ | : / : | V : / : V 1 2 ---- 3 4 ^ ^ / | | / Z ----> XX--->XX Mostly this is a question about understanding how APPEND works, namely, /not/ by mutation (that's APPEND!), but by /copying/ the spine pairs of all but the last argument -- in this case, copying X but not copying Y. (define x (list 1 2)) (define y (list 3 4)) (define z (cons x y)) (set-car! (cdr x) 5) (set-car! y 6) z Result: ((1 5) 6 4) Z ----> XX-------------------+ | | | | V V X ----> XX--->X/ Y -----> XX--->X/ | :\ :\ | V : \ : \ V 1 2 V 3 V 4 5 6 This box-and-pointer diagram should have been really easy; the only slight difficulty is knowing not to say ((1 5) . (6 4)) as 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
printed representation. (define x (list 1 2)) (define y (list 3 4)) (define z (cons x y)) (set! x 5) (set! y 6) z Result: ((1 2) 3 4) Z ----> XX-------------------+ | | | | V V X . ...> XX--->X/ Y . ....> XX--->X/ \ | | \ | | \ V V \ V V V 1 2 V 3 4 5 6 Here the crucial understanding is that changing the value of a /variable/ doesn't change the contents of any pairs, and that Z points to what the values of X and Y were at the time of the CONS, not to the names X and Y. Scoring: One point per diagram, one point per printed representation. 2. OOP to Scheme. (define make-foo (let ((a 3)) ; This LET happens when we define the class (lambda (b) ; This is MAKE-FOO, which makes an instance (let ((c 4)) ; This LET happens when we make an instance (define (d e) ; This is a method (+ a e)) (define (f g) ; This is the instance's dispatch procedure (if (eq? g 'h) d (error "huh?"))) f)))) ; MAKE-FOO returns the dispatch procedure! Class variable: A Instance variable: C Instantiation variable: B Message: G and H Method: D Dispatch procedure: F Method argument: E Instance: F G is a variable whose value will be the message that the user sends; H is a
Background image of page 2
literal (quoted) symbol, so it's a particular message that the object accepts. So "message" was the question with two answers. F is the dispatch procedure; it takes a message as argument and returns a method. But it's also the instance itself, since an instance is represented by its dispatch procedure. So "F" was the answer with two questions. We accepted B as an answer to "instance variable" on the theory that an instantiation variable is a kind of instance variable. But you should understand the difference: An instance variable gets its initial value from the class definition; in this case, every instance of the FOO class has a variable C whose initial value is 4. An instantiation variable gets its initial value from an argument to INSTANTIATE (in our OOP language) or to the class instantiation procedure MAKE-FOO (in the below-the-line implementation). Scoring:
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.

This note was uploaded on 01/15/2011 for the course CS 61A taught by Professor Harvey during the Fall '08 term at Berkeley.

Page1 / 11

cs61a_sp09_mt3_sol - CS 61A 1 Midterm 3 solutions Spring...

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