final - CS 61A Fall 2009 Final exam solutions 1 List...

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

View Full Document Right Arrow Icon
CS 61A Fall, 2009 Final exam solutions 1. List mutation vs. variable binding > (define x '(1)) > (define y '(2)) > (define z '(3)) > (set! y z) > (set! x y) > (set! z '(4)) > x SET! doesn't mutate any pairs; it just changes the binding of a variable. So, the first SET! changes Y to (3); the next changes X to (3); and the third changes Z to (4). This last one has no effect on X and Y; they remember their /value/, not whatever expression gave rise to the value. Answer: (3) > (define mylist (list 2 4 6 8)) > (define (magic! ls) (set-car! (cdr ls) 100) (set! ls (cons 1 ls)) (set-car! (cdr ls) 4)) > (magic! mylist) okay > mylist This time we /do/ mutate pairs. When we call MAGIC!, the variable LS is initially bound to the value of MYLIST, namely (2 4 6 8). The first SET-CAR! changes the car of (4 6 8) (which is (CDR LS)) to 100, so LS is now (2 100 6 8) and so is MYLIST, which is bound to the same pair. The SET! gives LS the value (1 2 100 6 8), /without/ changing MYSLIST. (CDR LS) is (2 100 6 8), so SET-CAR!ing that makes LS (1 4 100 6 8). But that first element isn't part of MYLIST. So we get Answer: (4 100 6 8) Scoring: 2 points each, all or nothing. 2. Dynamic vs. lexical scope > (define y 6) > (define z 3) > (define (foo x) (set! y (+ x y z)) y) > (define (bar z) (foo 10)) > (bar (foo 7)) The environment diagram is almost the same regardless of scope rule, so let's start by drawing it: The bindings Y=6 and Z=3 are added to the global frame G. The third DEFINE has an implicit lambda, which makes the procedure
Background image of page 1

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

View Full DocumentRight Arrow Icon
P1: params (x), body (set! . ..) y, env G and adds the binding FOO=P1 to G. The fourth DEFINE similarly makes P2: params (z), body (foo 10), env G and adds the binding BAR=P2 to G. So far, everything has happened in the global environment, because we haven't called any procedures. Now we have to start being careful about scope. To evaluate (bar (foo y)) we first evaluate the subexpressions (with current environment G). The value of BAR is P2. (FOO 7) is a procedure call, which we evaluate in the same way: FOO (in G) is P1; 7 is self-evaluating. Now we call P1 with argument 7. To do that, we make a frame in which the formal parameter [X] is bound to the actual argument value [7]. Which frame does it extend? In lexical scope, it extends the environment in which it was created, namely G. In dynamic scope, it extends the current environment, which is also G. So, for this procedure call it doesn't matter which rule we're following. E1: X=7, extends G Now, with E1 as current environment, we evaluate the body of P1, which has two expressions. The first is (SET! Y (+ X Y Z)). We start by evaluating the procedure call (+ X Y Z). We find these bindings: + = addition primitive, in G X = 7, in E1 Y = 6, in G Z = 3, in G So the result of the addition primitive is 7+6+3 = 16. Now we do the SET!, which modifies the binding of Y, in G, to 16. G:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/12/2010 for the course CS 61A taught by Professor Harvey during the Spring '08 term at Berkeley.

Page1 / 12

final - CS 61A Fall 2009 Final exam solutions 1 List...

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

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