Higher-Order Functions: basic-level questions

If you need help reviewing Higher-Order Functions, take a look at these resources:

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

Conceptual Questions

Question 1

What is the definition of a higher-order function?

A function is a higher-order function if it satisfies at least one of the following:

  • It takes at least one function as an argument
  • It returns a function

Question 2

In the following code, what type of object (number, boolean, string, function) does foo return? What type of object should x be for this function to work?

def foo(x):
    def inner(y):
        return x(y)
    return inner

foo should return a function object (in particular, the function inner). x should also be a function, or else calling inner will result in an error (because it will try x(y)).

What would Python print?

Question 3

>>> def silly():
...     def rabbit(y):
...         return 'Tricks are for kids!'
...     print('Lucky Charms?')
...     return False
>>> a = silly()
______
>>> a
______
>>> a(5)
______

Question 4

>>> def func1(fn):
...     def inner():
...         return fn(2)
...     return inner
>>> def func2(fn):
...     def inner():
...         return fn(2)
...     return inner()
>>> func1(lambda x: x * x)
______
>>> func2(lambda x: x * x)
______

Question 5

>>> def dream1(totem):
...     def dream2(totem_guess):
...         print('I think my totem is a', totem_guess)
...         return totem_guess == totem
...     return dream2
>>> inception = dream1('top')
>>> inception
______
>>> inception('spinning top')
______

Environment Diagrams

Question 6

def my_strat(score):
    return score + 2

def play(strat):
    i, roll = 0, strat(0)
    while i < roll:
        result = my_strat(i)
        i += 1
    return i

result = play(my_strat)

# How many times do we call my_strat?
# Remember to label the frames with the intrinsic
# name of the functions

Question 7

def fun(x):
    return x**2

def time(y):
    y, x = 4, 5
    def fun(y):
        return y + x
    return fun

a = time(10)
b = a(2)

# Which fun is called?
# Which y is used?
# What type of object is a?

Question 8

def square(x):
    return x * x

def boom(fn):
    def bam(x):
        print(x)
        return fn(x)
    return bam

boom(square)
a = boom(square)
a(4)

Question 9

x = 4

def outer(f):
    def inner(g):
        return f(g(x))
    return inner

def square(x):
    return x**2

c = outer(square)(square)

Question 10

def one(f):
    a = 1
    def two(g):
        b = 2
        def three(h):
            c = 3
            return f(a) + g(b) + h(c)
        return three
    return two

def identity(x):
    return x

def square(x):
    return x**2

def cube(x):
    return x**3

middle = one(identity)(square)
result = middle(cube)

# what function is middle?
# What are the parents of each frame?

Code-Writing questions

Question 11

Write a function make_mod that takes a number, n, as an argument, and returns a new function. The new function should take a single argument, x, and return the result of x modulo n.

def make_mod(n):
    """Returns a function that takes an argument x.
    That function will return x modulo n.

    >>> mod_7 = make_mod(7)
    >>> mod_7(3)
    3
    >>> mod_7(41)
    6
    """
    "*** YOUR CODE HERE ***"
def make_mod(n):
    def mod_n(x):
        return x % n
    return mod_n