First-Class Functions

The questions below are due on Sunday July 20, 2025; 10:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Consider the below program, separated into pieces. Indicate the value of the prompted expressions. If evaluating the expression would cause an error, write error in the box. If an expression is a function, write function in the box.

An environment diagram may be particularly helpful here.

def foo(x):
    return x * x

first = foo

def foo(y, f):
    return f(y + y)

a = first(3)
b = foo(5, first)

What's the value of a?

What's the value of b?

nums = [1, 5, 2, 4]
stats = {}

def foo(d, k, v):
    d[k] = v(nums)

a = foo(stats, "total", sum)
b = foo(stats, "lo", min)
c = foo(stats, "hi", max)
d = [a, b, c]

What's the value of d?

What's the value of stats?

def exponent(x, y=2):
    return x ** y

What's the value of exponent?

What's the value of y?

What's the value of exponent(6)?

What's the value of exponent(6, 0)?

What's the value of exponent(6, 0, 9)?

What's the value of exponent(y=2, x=3)?

Next Exercise: Dictionary Map

Back to exercises