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_tenpoints 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_tenor λ)? Which frame is the parent off2? - What value is the variable
resultbound 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_tenpoints to is λ (specifically, the lambda whose parameter isk). The parent frame of this lambda isf1. f2is labeled with the name λ the parent frame off2isf1, since that is where λ is defined.- The variable
resultis bound to 19.