Iterate

From HaskellWiki
Revision as of 01:34, 20 October 2006 by CaleGibbard (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to 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)