0

如何iterate修改功能以使结果为

f x, (f^2)x, (f^4)x, (f^8)x, ...

如果有人可以向我提供任何建议,我将非常高兴。

4

2 回答 2

4

Given, that f^x means f x-times applied to x I would say

iterate :: (a -> a) -> a -> [a]
iterate f x = f x : iterate (f . f) x

would suffice.

于 2012-06-11T18:25:36.673 回答
2

选择:

Prelude> map snd $ iterate (\(f,x) -> (f.f, f x)) ((+1),1)
[1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,...
于 2012-06-11T20:10:02.400 回答