Higher Order Functions

The Name Game (su16-mt1-2)

Draw the the environment diagram that results from executing the code below until the entire program is finished or an error occurs.

def person(name, age):
  def brain(ask):
    if ask == 'name':
      return name
    elif ask == 'age':
      return age
  return brain
def name(guy):
    return guy('name')
def age(gal):
    return gal('age')
my_name, my_age = 'Marv', 21
marv = person(my_name, my_age)
dan = person('Dan', age(marv) + 7)

The Name Game (fa14-mt1-2)

  1. Draw the the environment diagram that results from executing the code below until the entire program is finished or an error occurs.

    def peace(today):
        harmony = love + 2
        return harmony + today(love + 1)
    def joy(peace):
        peace, love = peace + 2, peace + 1
        return love // harmony
    love, harmony = 3, 2
    peace(joy)
  2. Draw the the environment diagram that results from executing the code below until the entire program is finished or an error occurs.

    def k(g, b):
      def n(s, a):
        return g - p
      return b(n(b, p))
    g, p = 3, 7
    k(p + 1, lambda s: g + 3)

Environmental Policy (sp16-mt1-2)

Draw the the environment diagram that results from executing the code below until the entire program is finished or an error occurs.

y = 3
def out(h, m):
  y = 5 * m
  def inner():
    return y
  if m == 0:
    return h
  else:
    return out(inner, m - 1)
v = out(None, 1)()

Vulcans (sp15-mt2-2)

  1. Draw the environment diagram that results from executing the code after the entire program is finished. No errors occur during the execution of this example.

    def scramble(egg):
      return [egg, over(egg)]
    
    def over(easy):
      easy[1] = [[easy], 2]
      return list(easy[1])
    
    egg = scramble([12, 24])

Pointers (sp16-mt2-1)

For each of the following code fragments, draw the final state of the program in boxes and arrows.