0

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.

0

2 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.