2.4 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.4.1 Assignment Statements

  1. Evaluate the expression on the right hand side of the = sign.
  2. 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.4.2 Def Statements

  1. 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.
  2. 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.4.3 Call Expressions

Note: you do not have to go through this process for a built-in Python function like max or print.

  1. Evaluate the operator, whose value should be a function.
  2. Evaluate the operands left to right.
  3. Open a new frame. Label it with the sequential frame number, the intrinsic name of the function, and its parent.
  4. Bind the formal parameters of the function to the arguments whose values you found in step 2.
  5. 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)