7/17/2011 1 CS61A Lecture 16 & 17 2011-07-18 Colleen Lewis 1 Environment Diagrams • Tedious algorithm – But it helps us keep track of how scheme ACTUALLY evaluates expressions • Always re-write syntactic sugar • Always re-write let • Follow the rules 5 IIB2. “define adds a new binding to the current frame” STk>(define A 3) Global A: 3 Current frame: Global The current frame always starts as the Global frame. E.g. the STk prompt is the global environment This is a frame (also known as an environment) 6 IIB1. “lambda creates a procedure” STk>(define A 3) STk>(define sq (lambda (x) (* x x))) Current frame: Global • Left bubble points to the formal parameters & body Params: x Body: (+ x x) • Right bubble points to the current environment Global A: 3 sq: IIB2. “define adds a new binding to the current frame” 7 Procedure Call IIA Step1. “evaluate the arguments” IIA Step2. • (a1) draw a frame • (a1) bind the formal parameters • (a2) extend environment the R bubble points to • (a3) evaluate the body Current frame: Global Params: x Body: (+ x x) Global A: 3 sq: STk>(sq 4) E1 x: 4 Current frame: Global E1 8 Procedure Call IIA Step1. “evaluate the arguments” IIA Step2. • (a1) draw a frame • (a1) bind the formal parameters • (a2) extend environment the R bubble points to • (a3) evaluate the body Current frame: Global Params: x Body: (+ x x) Global A: 3 sq: STk>(sq 4) STk>(sq A) E1 x: 4 E2 x: 3 Current frame: Global E2 97/17/2011 2 Procedure Call IIA Step1. “evaluate the arguments” IIA Step2. • (a1) draw a frame • (a1) bind the formal parameters • (a2) extend environment the R bubble points to • (a3) evaluate the body Current frame: Global Params: x Body: (+ x x) Global x: 4 sq: STk>(sq x) E1 x: 4 Current frame: Global E1 13 THE RULES (so far) • IIB2. “define adds a new binding to the current frame” • IIB1. “lambda creates a procedure” Procedure Call IIA Step1. “evaluate the arguments” IIA Step2. • (a1) draw a frame • (a1) bind the formal parameters • (a2) extend environment the R bubble points to • (a3) evaluate the body 14 STk> (define (fact n) (if (< n 2) n (* n (fact (- n 1))))) STk> (fact 3) FIRST: rewrite without Syntactic Sugar!!!! How many environments did you have? A) 1 (just global) B) 2 (E1 extends global) C) 3 (E1 & E2 extend global) D) 3 (E1 extends global & E2 extends E1) Draw the environment diagram for this… 16 Draw the environment diagram for this… STk> (define (fact n) (if (< n 2) n (* n (fact (- n 1))))) STk> (fact 3) FIRST: rewrite without Syntactic Sugar!!!! 17 IIB1. “lambda creates a procedure” STk>(define fact (lambda (n) (if (< n 2) n (* n (fact (- n 1))))) Current frame: Global • Left bubble points to the formal parameters & body Params: n Body: (if (< n 2) n (* n (fact (- n 1))) • Right bubble points to the current environment Global fact: 18 Procedure Call IIA Step1. “evaluate the arguments” IIA Step2. • (a1) draw a frame • (a1) bind the formal parameters • (a2) extend environment the R bubble points to • (a3) evaluate the body Current frame: Global Global fact: STk>(fact 3) E1 n: 3 Current frame: Global E1 Params: n Body: (if (< n 2) n (* n (fact (- n 1))) 197/17/2011 3 Params: n Body: (if (< n 2) n (* n (fact (- n 1))) Procedure Call IIA Step1. “evaluate the arguments” IIA Step2. • (a1) draw a frame • (a1) bind the formal parameters • (a2) extend environment the R bubble points to • (a3) evaluate the body Current frame: Global E1 Global fact: STk>(fact 3) E1 n: 3 Current frame: Global E1 E2 E2 n: 2 20 Things that we don’t keep track of • Return values • Pending calculations – e.g. (fact 3) was waiting for (fact 2) to finish. • Current environment – officially it is not tracked – but we recommend you do! 21 Where can we access a and b? STk> (define (mystery x y) (define a 3) (define b 4)) STk> a Is this an error? A) Yes B) No C) I don’t know STk> (define mystery (lambda (x y) (define a 3) (define b 4))) 23 IIB1. “lambda creates a procedure” STk>(define mystery (lambda (x y) (define a 3) (define b 4))) Current frame: Global • Left bubble points to the formal parameters & body Params: x y Body: (define a 3)... • Right bubble points to the current environment Global mystery: IIB2. “define adds a new binding to the current frame” 24 Where can we access a and b? STk> (define (mystery x y) (define a 3) (define b 4)) STk> (mystery 1 2) STk> a Is this an error? A) Yes B) No C) I don’t know 25 Procedure Call IIA Step1. “evaluate the arguments” IIA Step2. • (a1) draw a frame • (a1) bind the formal parameters • (a2) extend environment the R bubble points to • (a3) evaluate the body Current frame: Global Global mystery: STk>(mystery 1 2) E1 x: 1 y: 2 a: 3 b: 4 Current frame: Global E1 Params: x y Body: (define a 3)... IIB2. “define adds a new binding to the current frame” 267/17/2011 4 Draw the environment diagram STk>(define (rockin x) (define sq (lambda (x)(* x x))) (define b (+ x x)) b) STk>(rockin 3) The first step is to: A) Rewrite without syntactic sugar B) Draw bubbles C) Draw a frame The procedure defined as sq points to: A) Global B) E1 C) E2 27 IIB1. “lambda creates a procedure” Current frame: Global • Left bubble points to the formal parameters & body Params: x Body: (define sq ... • Right bubble points to the current environment Global rockin: 28 Procedure Call IIA Step1. “evaluate the arguments” IIA Step2. • (a1) draw a frame • (a1) bind the formal parameters • (a2) extend environment the R bubble points to • (a3) evaluate the body Current frame: Global Global rockin: STk>(rockin 3) E1 x: 3 Current frame: Global E1 Params: x Body: (define sq ... 29 Params: x Body: (define sq (λ(x)(* x x))) (define b (+ x x) • Left bubble points to the formal parameters & body • Right bubble points to the current environment Current frame: Global Global rockin: STk>(rockin 3) E1 x: 3 sq: b: 9 Current frame: Global E1 IIB1. “lambda creates a procedure” Params: x Body: (* x x) 30 Params: x Body: (define sq (λ(x)(* x x))) (define b (sq (+ 1 x))) Current frame: Global Global rockin: E1 x: 3 sq: b: 16 Current frame: Global E1 Params: x Body: (* x x) Procedure Call IIA Step1.
View Full Document