Iterate: Difference between revisions

From HaskellWiki
No edit summary
 
(category)
 
Line 18: Line 18:
iterate f x = fix ((x:) . map f)
iterate f x = fix ((x:) . map f)
</haskell>
</haskell>
[[Category:Code]]
[[Category:Glossary]]

Latest revision as of 04:20, 20 October 2006

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)