2.2 Environment Diagrams
Environment diagrams are one of the best learning tools for understanding lambda
expressions and higher order functions because you're able to keep track of all the different names, function objects, and arguments to functions. We highly recommend drawing environment diagrams or using Python tutor if you get stuck doing the WWPD problems below. For examples of what environment diagrams should look like, try running some code in Python tutor. Here are the rules:
2.2.1 Assignment Statements
- Evaluate the expression on the right hand side of the
=
sign. - If the name found on the left hand side of the
=
doesn't already exist in the current frame, write it in. If it does, erase the current binding. Bind the value obtained in step 1 to this name.
If there is more than one name/expression in the statement, evaluate all the expressions first from left to right before making any bindings.
Try to paste the following code in the Python tutor and understand each execution step. You can also click this link.
x = 10
y = x
x = 20
x, y = y + 1, x - 1
2.2.2 Def Statements
- Draw the function object with its intrinsic name, formal parameters, and parent frame. A function's parent frame is the frame in which the function was defined.
- If the intrinsic name of the function doesn't already exist in the current frame, write it in. If it does, erase the current binding. Bind the newly created function object to this name.
Try to paste the following code in the Python tutor and understand each execution step. You can also click this link.
def f(x):
return x + 1
def g(y):
return y - 1
def f(z):
return x * 2
2.2.3 Call Expressions
Note: you do not have to go through this process for a built-in Python function like
max
or
- Evaluate the operator, whose value should be a function.
- Evaluate the operands left to right.
- Open a new frame. Label it with the sequential frame number, the intrinsic name of the function, and its parent.
- Bind the formal parameters of the function to the arguments whose values you found in step 2.
- Execute the body of the function in the new environment.
Try to paste the following code in the Python tutor and understand each execution step. You can also click this link.
def f(a, b, c):
return a * (b + c)
def g(x):
return 3 * x
f(1 + 2, g(2), 6)
2.2.4 Lambdas
Note: As we saw in the
lambda
expression section above,lambda
functions have no intrinsic name. When drawinglambda
functions in environment diagrams, they are labeled with the namelambda
or with the lowercase Greek letter λ. This can get confusing when there are multiple lambda functions in an environment diagram, so you can distinguish them by numbering them or by writing the line number on which they were defined.
- Draw the lambda function object and label it with λ, its formal parameters, and its parent frame. A function's parent frame is the frame in which the function was defined.
This is the only step. We are including this section to emphasize the fact that the difference between lambda
expressions and def
statements is that lambda
expressions do not create any new bindings in the environment.
Try to paste the following code in the Python tutor and understand each execution step. You can also click this link.
lambda x: x * x #no binding created
square = lambda x: x * x
square(4) #calling a lambda function