Q3: Lambdas and Currying
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 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.
1 | def lambda_curry2(func): |
Q4: Make Adder
Draw the environment diagram for the following code:
1 | n = 9 |
There are 3 frames total (including the Global frame). In addition, consider the following questions:
In the Global frame, the name add_ten points to a function object. What is the intrinsic name of that function object, and what frame is its parent?
In frame f2, what name is the frame labeled with (add_ten or λ)? Which frame is the parent of f2?
What value is the variable result bound to in the Global frame?
The intrinsic name of the function object that add_ten points to is λ (specifically, the lambda whose parameter is k). The parent frame of this lambda is f1.
f2 is labeled with the name λ the parent frame of f2 is f1, since that is where λ is defined.
The variable result is bound to 19.
Q5: Lambda the Environment Diagram
Try drawing an environment diagram for the following code and predict what Python will output.
You do not need to submit or unlock this question through Ok. Instead, you can check your work with the Online Python Tutor, but try drawing it yourself first!
1 | >>> a = lambda x: x * 2 + 1 |
Q6: Composite Identity Function
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 compose1 function defined below.
1 | def compose1(f, g): |
Q7: Count van Count
Consider the following implementations of count_factors and count_primes:
1 | def count_factors(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 counts all the numbers from 1 to n that satisfy condition.
1 | def count_cond(condition): |
Q8: I Heard You Liked Functions…
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, return x
- n = 1, apply f1 to x, or return f1(x)
- n = 2, apply f1 to x and then f2 to the result of that, or return f2(f1(x))
- n = 3, apply f1 to x, f2 to the result of applying f1, and then f3 to the result of applying f2, or f3(f2(f1(x)))
- n = 4, start the cycle again applying f1, then f2, then f3, then f1 again, or f1(f3(f2(f1(x))))
And so forth.
Hint: most of the work goes inside the most nested function.
1 | def cycle(f1, f2, f3): |