From HaskellWiki
Jump to: navigation, search

This page documents some ways in which the Haskell prelude function iterate can be implemented.

First, the direct recursive way seen in the Haskell report:

iterate f x = x : iterate f (f x)

We can also write it in terms of scanl or scanl1 and repeat:

iterate f x = scanl f x (repeat x)
iterate f x = scanl1 f (repeat x)

Or in terms of fix:

iterate f x = fix ((x:) . map f)