Write a Python function power(f,n) that takes a function f and an integer n, and returns a function that applies the function f to its argument n times. For example: power(f,0)(x) = x # apply f over x zero time = no application power(f,2)(x) = f(f(x)) # apply f over x twice, same as twice(f) power(f,5)(x) = f(f(f(f(f(x))))) # apply f over x five times If we have the function: def incr(x): return x+1, and x=5, then the above three calls would be, power(incr,0)(5) # => 5 power(incr,2)(5) # => 7 (apply incr over 5 twice) power(incr,5)(5) # => 10 (apply incr over 5 five times)

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question

Write a Python function power(f,n) that takes a function f and an integer n, and returns a function that applies the function f to its argument n times. For example:

power(f,0)(x) = x # apply f over x zero time = no application

power(f,2)(x) = f(f(x)) # apply f over x twice, same as twice(f)

power(f,5)(x) = f(f(f(f(f(x))))) # apply f over x five times

If we have the function: def incr(x): return x+1, and x=5, then the above three calls would be,

power(incr,0)(5) # => 5

power(incr,2)(5) # => 7 (apply incr over 5 twice)

power(incr,5)(5) # => 10 (apply incr over 5 five times)

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning