DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 16 & 17

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

Berkeley COMPSCI 61A - Lecture 16 & 17

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture 16 & 17
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 16 & 17 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 16 & 17 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?