如何调用相同的函数'n'次? [英] How to call the same function 'n' times?
问题描述
可能存在重复:
库函数自己编写一个函数n次
我需要一个函数调用另一个函数n次。
所以它看起来像这样
fn = g(g(g(g(l))))
其中n等于嵌套的函数g的数量。
我应该怎么做?谢谢!
iterate
是一个常见的解决方案:
> :t迭代
迭代::(a - > a) - > a - > [a]
因此,给定一个与其范围相同的函数 a - >>一个
和一个初始输入 a
,产生一个无限的结果列表:
iterate fa - > [a,f(a),f(f(a)),...]
您可以使用 NB
pre $ 迭代fa! n
iterate f a !! 0 == a
。
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!
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
.
这篇关于如何调用相同的函数'n'次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!