Lambda Expressions: exam-level questions

If you need help reviewing Lambda Expressions, take a look at these resources:

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

Code-Writing questions

Question 1

Fill in the blanks for the following expression so that result is the number 42.

x = lambda x, y: lambda: x - y
result = (lambda ____, question: one(__________)(x, 4)
result = (lambda one, question: one(46, question)())(x, 4)

Question 2

Fill in the blanks for the following expression so that result is the boolean True.

x = lambda x: lambda y: x(y)
result = (lambda ______: x(fair)(dice))(lambda fair: fair == 3, 3)
result = (lambda fair, dice: x(fair)(dice))(lambda fair: fair == 3, 3)

Fill in the blanks for the following expression so that each call to mapper prints the output displayed below:

>>> def mapper(fn, num):
...     i = 0
...     while i < num:
...         print(fn(i))
...         i = i + 1
>>> mapper(lambda x: ______, 4)
1
3
5
7
>>> mapper(lambda x: ______, 5)
-2
-1
0
1
2
>>> mapper(lambda x: ______, 5)
0
-1
1
-2
2
mapper(lambda x: 2 * x + 1, 4)
mapper(lambda x: x - 2, 5)
mapper(lambda x: (x + 1) * (-1) ** x // 2, 5)

Environment Diagrams

Question 3

f = lambda x: lambda y: lambda z: g(x + y + z)

g = f(3)
f(4)(5)(6)

Question 4

fn = lambda f, a: f(f(2*a))

result = fn(lambda x: x*x, 2)

Question 5

fn = lambda: lambda: print('hi')

def example(x):
    print('example')
    return x

result = example(fn())()