Possible Duplicate:
Library function to compose a function with itself n times
I need a function to call another function n number of times.
so it would look something like this f n = g(g(g(g(l)))) where n equals to the number of function g nested.
how should I go about this? thanks!
The most common way to repeat a specific task or operation N times is by using the for loop in programming. We can iterate the code lines N times using the for loop with the range() function in Python.
Use the oml. index_apply function to run a Python function multiple times in Python engines spawned by the database environment. The times argument is an int that specifies the number of times to run the func function. The func argument is the function to run.
replicate takes an Int and a value, and returns a list that has several repetitions of the same element.
iterate
is a common solution:
> :t iterate iterate :: (a -> a) -> a -> [a]
So, given a function with a domain the same as its range, a -> a
, and an initial input a
, produce an infinite list of results in the form:
iterate f a --> [a, f(a), f(f(a)), ...]
And you can access the nth element of the list using !!
:
iterate f a !! n
NB iterate f a !! 0 == a
.
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