Higher Order Functions in Environment Diagrams
Note: this problem is quite long. It’s very good practice though and if you can successfully complete this problem, you are pretty solid on environment diagrams! Draw out the following environment diagram:
def f(x):
def y(f):
return f(x)
return y
def g(x):
return f(x)
def h(x):
return x * 2
def z(s):
return s + 2
y = g(5)
z = y(z)
p = y(h)
Here’s a visualization of the environment diagram. If you are stuck, remember the rules of environment diagrams.
The number one thing to remember is that when you call a function, you always need to make a new frame! And, also remember that functions have to keep track of their parent frames when they are not defined in the global frame. What that means is the nested function y(f)
has a parent frame because it’s defined inside of the f
frame.
I don't claim to be perfect so if you find an error on this page, please send me an email preferably with a link to this page so that I know what I need to fix!