I have been trying without much success to implement a higher order function that would take in repeat(f, n) where f is another function and n is the integer value that says how many times n will be applied to a variable x. For example:
def integer(x):
x + 1
so i would have repeat (integer, 5) and I would have integer(integer(integer(integer(integer(x)
You can use a simple for loop.
>>> def integer_inc(x):
... return x + 1
...
>>> def repeat(func, n, x):
... for i in range(n):
... x = func(x)
... return x
...
>>> repeat(integer_inc, 5, 1)
6
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With