如何调用同一个函数n次?(复制)

时间:2022-08-23 23:56:29

Possible Duplicate:
Library function to compose a function with itself n times

可能的重复:库函数,用自己n次组成一个函数

I need a function to call another function n number of times.

我需要一个函数调用另一个函数n次。

so it would look something like this f n = g(g(g(g(l)))) where n equals to the number of function g nested.

它看起来像这样f n = g(g(g(g(l))) n等于函数g嵌套的个数。

how should I go about this? thanks!

我该怎么做呢?谢谢!

4 个解决方案

#1


56  

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:

因此,给定一个与域相同的函数,a -> a和初始输入a,会产生一个无限的结果列表:

iterate f a --> [a, f(a), f(f(a)), ...]

And you can access the nth element of the list using !!:

您可以使用!! !访问列表的第n个元素!

iterate f a !! n

NB iterate f a !! 0 == a.

NB迭代fa !!0 = =。

#2


7  

This is a function that I use often at the ghci prompt. There are a few ways to write it, none of which I am particularly fond of, but they are all reasonably clean:

这是我在ghci提示符中经常使用的函数。有几种写法,没有一种是我特别喜欢的,但它们都相当干净:

fpow n f x = iterate f x !! n

fpow n f = foldr (.) id $ replicate n f

fpow n = foldr (.) id . replicate n   -- just eta the above

fpow 0 f = id
fpow n f = f . fpow (n-1) f

The middle two appeal to me because my brain has chunked foldr (.) id to mean "compose a list of functions".

中间的两个对我很有吸引力,因为我的大脑把foldr (.) id分成了“组成一个功能列表”的意思。

I kinda just wish it were in the prelude :-).

我只是希望它是在序曲:)。

#3


6  

f 0 = l
f n = g (f (n-1))

But more functional would be:

但功能更强的是:

f 0 l = l
f n l = g (f (n-1) l)

This could also be done with folds or morfisms, but this is easier to understand.

这也可以通过折叠或morfisms来完成,但这更容易理解。

For example here's using a hylomorphism, but it doesn't make it clearer really:

例如,这里使用的是低同构,但这并不能使它更清晰:

f g l = hylo l (.) (\n -> (g, n-1)) (==0)

It says some thing like compose (.) g(l) until n==0

它说了一些东西,比如写(。)g(l)直到n= 0。

#4


3  

Can be done using fold:

可以使用折叠:

applyNTimes :: Int -> (a -> a) -> a -> a
applyNTimes n f val = foldl (\s e -> e s) val [f | x <- [1..n]]

#1


56  

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:

因此,给定一个与域相同的函数,a -> a和初始输入a,会产生一个无限的结果列表:

iterate f a --> [a, f(a), f(f(a)), ...]

And you can access the nth element of the list using !!:

您可以使用!! !访问列表的第n个元素!

iterate f a !! n

NB iterate f a !! 0 == a.

NB迭代fa !!0 = =。

#2


7  

This is a function that I use often at the ghci prompt. There are a few ways to write it, none of which I am particularly fond of, but they are all reasonably clean:

这是我在ghci提示符中经常使用的函数。有几种写法,没有一种是我特别喜欢的,但它们都相当干净:

fpow n f x = iterate f x !! n

fpow n f = foldr (.) id $ replicate n f

fpow n = foldr (.) id . replicate n   -- just eta the above

fpow 0 f = id
fpow n f = f . fpow (n-1) f

The middle two appeal to me because my brain has chunked foldr (.) id to mean "compose a list of functions".

中间的两个对我很有吸引力,因为我的大脑把foldr (.) id分成了“组成一个功能列表”的意思。

I kinda just wish it were in the prelude :-).

我只是希望它是在序曲:)。

#3


6  

f 0 = l
f n = g (f (n-1))

But more functional would be:

但功能更强的是:

f 0 l = l
f n l = g (f (n-1) l)

This could also be done with folds or morfisms, but this is easier to understand.

这也可以通过折叠或morfisms来完成,但这更容易理解。

For example here's using a hylomorphism, but it doesn't make it clearer really:

例如,这里使用的是低同构,但这并不能使它更清晰:

f g l = hylo l (.) (\n -> (g, n-1)) (==0)

It says some thing like compose (.) g(l) until n==0

它说了一些东西,比如写(。)g(l)直到n= 0。

#4


3  

Can be done using fold:

可以使用折叠:

applyNTimes :: Int -> (a -> a) -> a -> a
applyNTimes n f val = foldl (\s e -> e s) val [f | x <- [1..n]]