Question 1: Make Adder
Draw the environment diagram for the following code:
n = 9
def make_adder(n):
return lambda k: k + n
add_ten = make_adder(n+1)
result = add_ten(n)
There are 3 frames total (including the Global frame). In addition, consider the following questions:
- In the Global frame, the name
add_ten
points to a function object. What is the intrinsic name of that function object, and what frame is its parent? - In frame
f2
, what name is the frame labeled with (add_ten
or λ)? Which frame is the parent off2
? - What value is the variable
result
bound to in the Global frame?
You can try out the environment diagram at tutor.cs61a.org. To see the environment diagram for this question, click here.
- The intrinsic name of the function object that
add_ten
points to is λ (specifically, the lambda whose parameter isk
). The parent frame of this lambda isf1
. f2
is labeled with the name λ the parent frame off2
isf1
, since that is where λ is defined.- The variable
result
is bound to 19.