How to create an infinite lazy list of function applications?

Submitted by metaperl on Thu, 10/06/2005 - 12:33pm.

I would like to create an infinite lazy list whose contents consist of:

[ x, f x, f (f x), f (f (f x)) ... ] -- ad infinitum.

How might I do this?

Submitted by metaperl on Thu, 10/06/2005 - 12:40pm.

[11:35:56] :t iterate
[11:35:56] iterate :: (a -> a) -> a -> [a]
[11:36:03] take 10 $ iterate (+1) 0
[11:36:03] [0,1,2,3,4,5,6,7,8,9]
[11:36:24] iterate f x = x : iterate f (f x)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.