Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Repeated Function Application Write a recursive function repeatedly_apply that t

ID: 3599585 • Letter: R

Question

Repeated Function Application Write a recursive function repeatedly_apply that takes as arguments a function f of one argument and a positive integer n. The result of repeatedly_apply is a function of one argument that applies f to that argument n times. So, for example, we would have repeatedly-apply(lambda x: x+1 , 10) (100) --> 110 You may assume that the following function has been defined. You don't have to use it, but it can contribute to a pretty solution. def compose(f,g): return lambda x: f(g(:x )) Hint: f"n (x) == f(FA (n-1) (x))-ie. f applied to x n times is the same as f applied to f applied to x n-1 times. Write repeatedly_apply Submitted: No g lambda x: x+1; repeatedly_apply(g, 1) (100) -> 101 g = lambda x: x+1 ; repeatedly-apply (g, 10)(100) -> 110 name 'repeatedly_apply' is not defined

Explanation / Answer

I have written the repeatedly_apply lambda function for the same. The idea is to use recursion for this and code the function for the same.

the compose function will be:

def compose (f,g):

if g > 1:

repeatedly_apply = compose(f, g - 1)

return lambda x: f(repeatedly_apply(x))

return f

Here we decrement the value of g by 1 and then repeadtedly call the function that many times to get the value.

A single line lambda function for this can also be written as:

repeatedly_apply = lambda f, n: lambda x: x if n == 0 else repeatedly_apply(f, n-1)(f(x))