{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

final

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

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

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

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

View Full Document
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: Y=16, Z=3, FOO=P1, BAR=P2 The second expression in the body of P1 is Y. We find the global binding Y=16, so (FOO 7) returns the value 16.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern