Make Repeater (100pts)
Implement the function make_repeater
so that make_repeater(h, n)(x)
returns h(h(...h(x)...))
,
where h is applied n times.
That is, make_repeater(h, n)
returns another function that can then be applied to another argument.
For example, make_repeater(square, 3)(42)
evaluates to square(square(square(42)))
.
def make_repeater(h, n):
"""Return the function that computes the nth application of h.
>>> add_three = make_repeater(increment, 3)
>>> add_three(5)
8
>>> make_repeater(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
243
>>> make_repeater(square, 2)(5) # square(square(5))
625
>>> make_repeater(square, 4)(5) # square(square(square(square(5))))
152587890625
>>> make_repeater(square, 0)(5) # Yes, it makes sense to apply the function zero times!
5
"""
"*** YOUR CODE HERE ***"
For an extra challenge, try defining
make_repeater
in a single one-line return statement using yourcompose
function (defined in problem 1) and youraccumulate
function (defined in problem 3).