Environment Diagrams: exam-level questions

If you need help reviewing Environment Diagrams, take a look at these resources:

Each question has a "Toggle Solution" button -- click it to reveal that question's solution.

Environment Diagrams

Question 1

def funny(joke):
    hoax = joke + 1
    return funny(hoax)

def sad(joke):
    hoax = joke - 1
    return hoax + hoax

funny, sad = sad, funny
result = funny(sad(1))

# pay special attention to the names of
# the frames!

Question 2

def double(x):
    return double(x + x)

first = double

def double(y):
    return y + y

result = first(10)

Question 3

def fun(fun):
    def time(time):
        return fun(x)
    x = 4
    return time

def boo(x):
    return x**2
    x = 5

result = fun(boo)(10)

Question 4

from operator import sub
def trick(me, you):
    sub = treat
    return sub

def treat(me, you):
    return sub(me, 1)

treat = trick
trick(3, 4)

Question 5

def easy(x):
    def peasy(y):
        def ironic(name):
            return name(x, y)
        return y
    return peasy

result = easy(4)(easy)(2)