1. Instructions
In this lab assignment, you have four tasks:
- Think about what python would display if the code described in section 3 were input to a python interpreter, better without directly running python.
- Draw environment diagrams for the code in section 4.
- Complete the mock exam problems in section 5.
- Complete the required problems described in section 6 and submit your code as instructed in lab00. The starter code for these problems is provided in
lab02.py
.
Submission: As instructed before, you need to submit your work with Ok by python ok --submit
.
You may submit more than once before the deadline,
and your score of this assignment will be the highest one of all your submissions.
Readings: You might find the following references to the textbook useful:
2. Review
Consult this section if you need a refresher on the material for this lab. It's okay to skip directly to the next section and refer back here when you get stuck.
2.1 Lambda Expressions
Lambda expressions are expressions that evaluate to functions by specifying two things: the parameters and a return expression.
lambda <parameters>: <return expression>
While both lambda
expressions and def
statements create function objects, there are some notable differences. lambda
expressions work like other expressions; much like a mathematical expression just evaluates to a number and does not alter the current environment, a lambda
expression evaluates to a function without changing the current environment. Let's take a closer look.
lambda | def | |
---|---|---|
Type | Expression that evaluates to a value | Statement that alters the environment |
Result of execution | Creates an anonymous lambda function with no intrinsic name. | Creates a function with an intrinsic name and binds it to that name in the current environment. |
Effect on the environment | Evaluating a lambda expression does not create or modify any variables. | Executing a def statement both creates a new function object and binds it to a name in the current environment. |
Usage | A lambda expression can be used anywhere that expects an expression, such as in an assignment statement or as the operator or operand to a call expression. | After executing a def statement, the created function is bound to a name. You should use this name to refer to the function anywhere that expects an expression. |
-
lambda example
# A lambda expression by itself does not alter # the environment lambda x: x * x # We can assign lambda functions to a name # with an assignment statement square = lambda x: x * x square(3) # Lambda expressions can be used as an operator # or operand negate = lambda f, x: -f(x) negate(lambda x: x * x, 3) # We can directly call a lambda expression # just created # Make sure the lambda expression wrapped in a # pair of parenthesis or between `(`, `,`, and `)` # in order not to let Python misunderstand you (lambda x: x * 2 ** x)(5) # evaluates to 160
-
def example
def square(x): return x * x # A function created by a def statement # can be referred to by its intrinsic name square(3)
2.2 Higher-Order Functions
Variables are names bound to values, which can be primitives like 3
or 'Hello World'
, but they can also be functions. And since functions can take arguments of any value, other functions can be passed in as arguments. This is the basis for higher-order functions.
A higher order function is a function that manipulates other functions by taking in functions as arguments, returning a function, or both. We will introduce the basics of higher order functions in this lab and will be exploring many applications of higher order functions in our next lab.
2.2.1 Functions as arguments
In Python, function objects are values that can be passed around. We know that one way to create functions is by using a def
statement:
def square(x):
return x * x
The above statement created a function object with the intrinsic name square
as well as binded it to the name square
in the current environment. Now let's try passing it as an argument.
First, let's write a function that takes in another function as an argument:
def scale(f, x, k):
""" Returns the result of f(x) scaled by k. """
return k * f(x)
We can now call scale
on square
and some other arguments:
>>> scale(square, 3, 2) # Double square(3)
18
>>> scale(square, 2, 5) # 5 times 2 squared
20
Note that in the body of the call to scale
, the function object with the intrinsic name square
is bound to the parameter f
. Then, we call square
in the body of scale
by calling f(x)
.
As we saw in the above section on lambda
expressions, we can also pass lambda
expressions into call expressions!
>>> scale(lambda x: x + 10, 5, 2)
30
In the frame for this call expression, the name f
is bound to the function created by the lambda
expression lambda x: x + 10
.
2.2.2 Functions that return functions
Because functions are values, they are valid as return values! Here's an example:
def multiply_by(m):
def multiply(n):
return n * m
return multiply
In this particular case, we defined the function multiply
within the body of multiply_by
and then returned it. Let's see it in action:
>>> multiply_by(3)
<function multiply_by.<locals>.multiply at ...>
>>> multiply(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'multiply' is not defined
A call to multiply_by
returns a function, as expected. However, calling multiply
errors, even though that's the name we gave the inner function. This is because the name multiply
only exists within the frame where we evaluate the body of multiply_by
.
So how do we actually use the inner function? Here are two ways:
>>> times_three = multiply_by(3) # Assign the result of the call expression to a name
>>> times_three(5) # Call the inner function with its new name
15
>>> multiply_by(3)(10) # Chain together two call expressions
30
The point is, because multiply_by
returns a function, you can use its return value just like you would use any other function.
2.3 Environment Diagrams
Environment diagrams are one of the best learning tools for understanding lambda
expressions and higher order functions because you're able to keep track of all the different names, function objects, and arguments to functions. We highly recommend drawing environment diagrams or using Python tutor if you get stuck doing the WWPD problems below. For examples of what environment diagrams should look like, try running some code in Python tutor. Here are the rules:
2.2.1 Assignment Statements
- Evaluate the expression on the right hand side of the
=
sign. - If the name found on the left hand side of the
=
doesn't already exist in the current frame, write it in. If it does, erase the current binding. Bind the value obtained in step 1 to this name.
If there is more than one name/expression in the statement, evaluate all the expressions first from left to right before making any bindings.
Try to paste the following code in the Python tutor and understand each execution step. You can also click this link.
x = 10
y = x
x = 20
x, y = y + 1, x - 1
2.2.2 Def Statements
- Draw the function object with its intrinsic name, formal parameters, and parent frame. A function's parent frame is the frame in which the function was defined.
- If the intrinsic name of the function doesn't already exist in the current frame, write it in. If it does, erase the current binding. Bind the newly created function object to this name.
Try to paste the following code in the Python tutor and understand each execution step. You can also click this link.
def f(x):
return x + 1
def g(y):
return y - 1
def f(z):
return x * 2
2.2.3 Call Expressions
Note: you do not have to go through this process for a built-in Python function like
max
or
- Evaluate the operator, whose value should be a function.
- Evaluate the operands left to right.
- Open a new frame. Label it with the sequential frame number, the intrinsic name of the function, and its parent.
- Bind the formal parameters of the function to the arguments whose values you found in step 2.
- Execute the body of the function in the new environment.
Try to paste the following code in the Python tutor and understand each execution step. You can also click this link.
def f(a, b, c):
return a * (b + c)
def g(x):
return 3 * x
f(1 + 2, g(2), 6)
2.2.4 Lambdas
Note: As we saw in the
lambda
expression section above,lambda
functions have no intrinsic name. When drawinglambda
functions in environment diagrams, they are labeled with the namelambda
or with the lowercase Greek letter λ. This can get confusing when there are multiple lambda functions in an environment diagram, so you can distinguish them by numbering them or by writing the line number on which they were defined.
- Draw the lambda function object and label it with λ, its formal parameters, and its parent frame. A function's parent frame is the frame in which the function was defined.
This is the only step. We are including this section to emphasize the fact that the difference between lambda
expressions and def
statements is that lambda
expressions do not create any new bindings in the environment.
Try to paste the following code in the Python tutor and understand each execution step. You can also click this link.
lambda x: x * x #no binding created
square = lambda x: x * x
square(4) #calling a lambda function
3. What Would Python Display?
In this section, you need to think about what python would display if the code given were input to a python interpreter.
Question 1: Lambda the Free
Submit your answers with
python ok -q q1 -u
.In What Would Python Display problems, always follow these rules:
For each of the expressions in the table below, write the output displayed by the interactive Python interpreter when the expression is valued. The output may have multiple lines or no lines.
- If an error occurs, write
Error
, but include all output displayed before the error.- If an expression evaluates to a function value, write
Function
.- If an expression would take forever to evaluate, write
Forever
.- If the interpretor prints nothing, write
Nothing
.
>>> lambda x: x # A lambda expression with one parameter x
______
>>> a = lambda x: x # Assigning the lambda function to the name a
>>> a(5)
______
>>> (lambda: 3)() # Using a lambda expression as an operator in a call exp.
______
>>> b = lambda x: lambda: x # Lambdas can return other lambdas!
>>> c = b(88)
>>> c
______
>>> c()
______
>>> d = lambda f: f(4) # They can have functions as arguments as well.
>>> def square(x):
... return x * x
>>> d(square)
______
>>> z = 3
>>> e = lambda x: lambda y: lambda: x + y + z
>>> e(0)(1)()
______
>>> f = lambda z: x + z
>>> f(3)
______
>>> x = None # remember to review the rules of WWPD given above!
>>> x
______
>>> higher_order_lambda = lambda f: lambda x: f(x)
>>> g = lambda x: x * x
>>> higher_order_lambda(2)(g)
______
>>> higher_order_lambda(g)(2)
______
>>> call_thrice = lambda f: lambda x: f(f(f(x)))
>>> call_thrice(lambda y: y + 1)(0)
______
>>> print_lambda = lambda z: print(z) # When is the return expression of a lambda expression executed?
>>> print_lambda
______
>>> one_thousand = print_lambda(1000)
______
>>> one_thousand
______
Question 2: Higher Order Functions
Submit your answers with
python ok -q q2 -u
.
>>> def even(f):
... def odd(x):
... if x < 0:
... return f(-x)
... return f(x)
... return odd
>>> steven = lambda x: x
>>> stewart = even(steven)
>>> stewart
______
>>> stewart(61)
______
>>> stewart(-4)
______
>>> def cake():
... print('beets')
... def pie():
... print('sweets')
... return 'cake'
... return pie
>>> chocolate = cake()
______
>>> chocolate
______
>>> chocolate()
______
>>> more_chocolate, more_cake = chocolate(), cake
______
>>> more_chocolate
______
>>> def snake(x, y):
... if cake == more_cake:
... return chocolate
... else:
... return x + y
>>> snake(10, 20)
______
>>> snake(10, 20)()
______
>>> cake = 'cake'
>>> snake(10, 20)
______
4. Environment Diagrams
In this section, you need to draw an environment diagram for the code given.
You don't have to submit your answers. But similar problems will appear on the exam.
Question 3: A, B, and X
Draw the environment diagram of the following code:
a = lambda x: x * 2 + 1
def b(b, x):
return b(x + a(x))
x = 3
b(a, x)
After drawing the diagram, compare it to the correct one here.
Then predict what would python display and submit your answer with python ok -q q3 -u
.
5. Mock Exam Problems
In this section, you have 30 minutes to complete two mock exam problems.
These problems are taken from midterm exam in 2021 and slightly modified.
6. Required Problems
In this section, you are required to complete the problems below and submit your code as instructed in lab00 to get your answer scored.
Problem 1: Lambdas and Currying (100pts)
We can transform multiple-argument functions into a chain of single-argument, higher order functions by taking advantage of lambda expressions. This is useful when dealing with functions that take only single-argument functions. We will see some examples of these later on.
Write a function lambda_curry2
that will curry any two argument function using lambdas. See the doctest or refer to Section 1.6.6 in the textbook if you're not sure what this means.
Your solution to this problem should fit entirely on the return line. You can try writing it first without this restriction, but rewrite it after in one line to test your understanding of this topic.
To test your implementation, run
python ok -q lambda_curry2
.
def lambda_curry2(func):
"""
Returns a Curried version of a two-argument function FUNC.
>>> from operator import add, mul, mod
>>> curried_add = lambda_curry2(add)
>>> add_three = curried_add(3)
>>> add_three(5)
8
>>> curried_mul = lambda_curry2(mul)
>>> mul_5 = curried_mul(5)
>>> mul_5(42)
210
>>> lambda_curry2(mod)(123)(10)
3
>>> # You aren't expected to understand the code of this test.
>>> # It's just here to check that definition of lambda_curry2 is just a return statement.
>>> import inspect, ast
>>> [type(x).__name__ for x in ast.parse(inspect.getsource(lambda_curry2)).body[0].body]
['Expr', 'Return']
"""
return ______
Problem 2: Count van Count (100pts)
Consider the following implementations of count_factors
and count_primes
:
def count_factors(n):
"""Return the number of positive factors that n has.
>>> count_factors(6)
4 # 1, 2, 3, 6
>>> count_factors(4)
3 # 1, 2, 4
"""
i, count = 1, 0
while i <= n:
if n % i == 0:
count += 1
i += 1
return count
def count_primes(n):
"""Return the number of prime numbers up to and including n.
>>> count_primes(6)
3 # 2, 3, 5
>>> count_primes(13)
6 # 2, 3, 5, 7, 11, 13
"""
i, count = 1, 0
while i <= n:
if is_prime(i):
count += 1
i += 1
return count
def is_prime(n):
return count_factors(n) == 2 # only factors are 1 and n
The implementations look quite similar! Generalize this logic by writing a function count_cond
, which takes in a two-argument predicate function condition(n, i)
. count_cond
returns a one-argument function that takes in n
, which counts all the numbers from 1 to n
that satisfy condition
when called.
To test your implementation, run
python ok -q count_cond
.
def count_cond(condition):
"""Returns a function with one parameter N that counts all the numbers from
1 to N that satisfy the two-argument predicate function Condition, where
the first argument for Condition is N and the second argument is the
number from 1 to N.
>>> count_factors = count_cond(lambda n, i: n % i == 0)
>>> count_factors(2) # 1, 2
2
>>> count_factors(4) # 1, 2, 4
3
>>> count_factors(12) # 1, 2, 3, 4, 6, 12
6
>>> is_prime = lambda n, i: count_factors(i) == 2
>>> count_primes = count_cond(is_prime)
>>> count_primes(2) # 2
1
>>> count_primes(3) # 2, 3
2
>>> count_primes(4) # 2, 3
2
>>> count_primes(5) # 2, 3, 5
3
>>> count_primes(20) # 2, 3, 5, 7, 11, 13, 17, 19
8
"""
"*** YOUR CODE HERE ***"
Problem 3: Composite Identity Function (50pts)
Write a function that takes in two single-argument functions, f
and g
, and returns another function that has a single parameter x
. The returned function should return True
if f(g(x))
is equal to g(f(x))
. You can assume the output of g(x)
is a valid input for f
and vice versa. You may use the composer
function defined below.
To test your implementation, run
python ok -q composite_identity
.
def composer(f, g):
"""Return the composition function which given x, computes f(g(x)).
>>> add_one = lambda x: x + 1 # adds one to x
>>> square = lambda x: x**2
>>> a1 = composer(square, add_one) # (x + 1)^2
>>> a1(4)
25
>>> mul_three = lambda x: x * 3 # multiplies 3 to x
>>> a2 = composer(mul_three, a1) # ((x + 1)^2) * 3
>>> a2(4)
75
>>> a2(5)
108
"""
return lambda x: f(g(x))
def composite_identity(f, g):
"""
Return a function with one parameter x that returns True if f(g(x)) is
equal to g(f(x)). You can assume the result of g(x) is a valid input for f
and vice versa.
>>> add_one = lambda x: x + 1 # adds one to x
>>> square = lambda x: x**2
>>> b1 = composite_identity(square, add_one)
>>> b1(0) # (0 + 1)^2 == 0^2 + 1
True
>>> b1(4) # (4 + 1)^2 != 4^2 + 1
False
"""
"*** YOUR CODE HERE ***"
Problem 4: I Heard You Liked Functions... (150pts)
Define a function cycle
that takes in three functions f1
, f2
, f3
, as arguments. cycle
will return another function that should take in an integer argument n
and return another function. That final function should take in an argument x
and cycle through applying f1
, f2
, and f3
to x
, depending on what n
was. Here's what the final function should do to x
for a few values of n
:
n = 0
, returnx
n = 1
, applyf1
tox
, or returnf1(x)
n = 2
, applyf1
tox
, and thenf2
to the result of that, or returnf2(f1(x))
n = 3
, applyf1
tox
,f2
to the result of applyingf1
, and thenf3
to the result of applyingf2
, orf3(f2(f1(x)))
n = 4
, start the cycle again applyingf1
, thenf2
, thenf3
, thenf1
again, orf1(f3(f2(f1(x))))
- And so forth.
Hint: most of the work goes inside the most nested function.
To test your implementation, run
python ok -q cycle
.Submit your code with
python ok --submit
.
def cycle(f1, f2, f3):
"""Returns a function that is itself a higher-order function.
>>> def add1(x):
... return x + 1
>>> def times2(x):
... return x * 2
>>> def add3(x):
... return x + 3
>>> my_cycle = cycle(add1, times2, add3)
>>> identity = my_cycle(0)
>>> identity(5)
5
>>> add_one_then_double = my_cycle(2)
>>> add_one_then_double(1)
4
>>> do_all_functions = my_cycle(3)
>>> do_all_functions(2)
9
>>> do_more_than_a_cycle = my_cycle(4)
>>> do_more_than_a_cycle(2)
10
>>> do_two_cycles = my_cycle(6)
>>> do_two_cycles(1)
19
"""
"*** YOUR CODE HERE ***"