Iterate

From HaskellWiki
Revision as of 04:20, 20 October 2006 by DonStewart (talk | contribs) (category)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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)