user721588
user721588

Reputation:

iterate function in haskell

How can be iterate function modified that the result will be

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

I'd be very happy if anybody could provide me with any suggestion.

Upvotes: 0

Views: 2469

Answers (2)

sdcvvc
sdcvvc

Reputation: 25644

Alternative:

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,...

Upvotes: 2

Teresa Siegmantel
Teresa Siegmantel

Reputation: 903

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.

Upvotes: 4

Related Questions