CS-Fall 2010 MT3 Solutions

CS-Fall 2010 MT3 Solutions - 12/11/2010

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

View Full Document Right Arrow Icon
GROUP QUESTION: Environment Diagrams (define x 10) adds the binding x=10 to the global frame G. (define (foo x) . ..) creates procedure P1 with params: (x) body: (if . ..) env: G and then creates the binding foo=P1 in the global frame G. (By the way, I'm writing "foo=P1" because I'm doing this in text format. When drawing an actual diagram, you want an arrow from the name foo to the procedure bubbles! No need to give procedures names like P1 at all.) Nothing else happens during this definition -- the body of P1 is not evaluated at this time. (foo 4) creates frame E1 with the binding x=4, extending G. With E1 as the current environment, evaluate the body, which is the IF expression. Since x=4 in this environment, the OR is false, so we evaluate the LET. This creates a procedure P2: params: (y z) body: (set! . ..) . .. env: E1 and then immediately invokes the procedure. First it has to evaluate the argument expressions 8 and (/ X 2). The first is self-evaluating. For the second, we find the binding x=4 in current environment E1, and divide that by 2, giving 2. (/ is a primitive, so there's no need to add anything to the diagram for this call.) So now we make a new frame E2 with bindings y=8, z=2, extending E1 (because that's where the right bubble of P2 points). With E2 as current environment, evaluate the body of the LET, which consists of three expressions: (set! y (lambda (z) (* z z))) The LAMBDA expression creates procedure P3: params: (z) body: (* z z) env: E2 and then changes the binding of y in E2 from 8 to that procedure (P3). It does not call the procedure! So it does not compute (* z z)! (set! x 14) Since the current environment is E2, and there is no binding for x in that frame, we look at the frame it extends, namely E1. There we do find a binding x=4, and we replace the 4 with 14. We don't change the global binding x=10! 12/11/2010 https://inst.eecs.berkeley.edu/~cs61a/fa… inst.eecs.berkeley.edu/~cs61a/…/mt3 1/10
Background image of page 1

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

View Full DocumentRight Arrow Icon
(foo z) We look for bindings for FOO (value P1, found in G) and Z (value 2, found in E2), then call P1 with argument 2. This creates a new frame E3, with x=2, **extending G** because that's where the right bubble of P1 points. With E3 as current environment, we evaluate the body of P1, the IF expression. This time, since x=2, the OR returns true, and the procedure returns the value of x, which is 2. Scoring: Almost all the groups got this right. The only really common mistake was to use dynamic scope, in which E3 extends E2. This got 3 points. 5 correct. 3 dynamic scope; slight right-bubble error (e.g. P2 -> E2). 2 change global x to 14; really bad right-bubble error; change y to 4. 1 multiple right-bubble errors; new frame for SET!. 0
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.

Page1 / 10

CS-Fall 2010 MT3 Solutions - 12/11/2010

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