Environment Diagrams
What are Environment Diagrams?
- A visual tool to keep track of binding & state of a computer program
Why do we use Environment Diagrams?
- Environment Diagrams are conceptual
: understand why programs work the way they do
: confidently predict how a program will behave
- Environment diagrams are helpful for debugging
: when you're really stuck, diagramming code > starting at lines of code
- Environment Diagrams will be used in future courses
: CS 61C, CS164
What do we've seen so far
: Assignment statements
Def statements
Call expressions
Terminology : Frames
- A frame keeps track of variable-to-value bindings
: every call expression has a corresponding frame
- Global, a.k.a the global frame, is the starting frame
: It doesn't correspond to specific function call
- Parent frame
: The parent of a function is the frame in which it was defined.
: If you can't find a variable in the current frame, you check it's parent and so on. If you can't find the variable, NameError
python codes ex.
y = 10
def broken_square(x) :
return x + y
broken_square(4)
explain. Global y = 10
broken_square -> func broken_square(x) [parent = Global]
f1 : broken_square [parent = Global]
x = 4
return value = 14
Check your understanding
def square(x) :
Return x * x
def sum_of_squares (x,y) :
Return square(x) + square(y)
sum_of_squares(3,4)
my thinking. sum_of_square -> func sum_of_square(x,y) [parent = square]
f1 : sum_of_square [parent = square]
square -> func square(x) [parent = Global]
return value = 25
f2 : square [parent = Global]
x = 9, y = 16
answer. Global sqaure -> func square ( x ) [ parent = Global ]
sum_of_squares -> func sum_of_squares ( x, y ) [ parent = Global ]
f1 : sum_of_squares [ parent = Global ]
x = 3, y = 4
return value = 25
f2 : square [ parent = Global ]
x = 3, return value = 9
f3 : square [ parent = Global ]
x = 4, return value = 16
Local Names
Variable lookeup :
- Lookup name in the current frame
- Lookup name in parent frame, its parent frame, etc ...
- Stop at the global frame
- If not found, an error is thrown
python code ex.
def f(x,y) :
return g(x)
def g(z):
return z + x
result = f (5,10)
explain.
Global frame f -> func f(x,y) [ parent = Global]
g -> func g(z) [ parent = Global ]
f1 : f [parent = Global ]
x = 5, y = 10
f2 : g [parent = Global ]
z = 5
f2 : Name "x" is not found
Global : Name "x" is not found
SO an error is thrown
Important : there was no lookup done in f1 since the parent of f2 was Global
Evaluation vs Apply
Drawing Environment Diagrams
- Option 1 : Python Tutor ( tutor.cs61a.org )
- Option 2 : PythonAnywhere ( editor.pythonanywhere.com )
Lambda Expressions
Expressions that evaluate to functions !
ex. squre = lambda x : x * x
lambda = A function , with parameter x, that returns the value of x * x
square
<function <lambda> ... >
when you call lambda expression it goes on like ordinary function
Lambda Expressions vs def Statements
lambda expression ex.
square = lambda x : x * x
Global frame
square -> func lambda(x) <line 1> [parent = Global]
def statement ex.
def square (x) :
return x * x
Global frame
square -> func square(x) [parent = Global]
- both create a function with the same behavior
- The parent frame of each function is the frame in which they were defined
- Both bind the function to the same name
- Only the def statement gives the function an intrinsic name
Locally Defined Functions
def make_greeter (name) :
return lambda greeting : print(greeting, name)
greeter_function = make_greeter ("Tiffany")
greeter_function("Hey what's up,")
explain.
Global frame : make_greeter -> lambda function
f1 : make_greeter [ parent = Global ]
name = "Tiffany"
Return value ->lambda function
Summary
-
'University of California, Berkeley > ElectricalEngineering & ComputerSciences' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Higher-Order Functions (0) | 2019.07.02 |
---|---|
Week 2 (0) | 2019.07.02 |
LAB_day 1 (0) | 2019.06.27 |
Control (0) | 2019.06.27 |
DISC (0) | 2019.06.27 |