1
votes

In the standard prelude:

Prelude> :t iterate
iterate :: (a -> a) -> a -> [a]

However, in classy prelude there is no iterate, so I presume there might be some more generic function to do the same, perhaps a monadic one. I just cannot figure out what it is. Is there one?

1
Generalized anamorphisms (like iterate or unfold) are less common than generalized catamorphisms (like map or fold). It think it's more likely iterate was omitted because it isn't used commonly enough to warrant inclusion in a redesigned prelude. - chepner

1 Answers

2
votes

You can always reimplement it with ClassyPrelude.repeat and Data.List.scanl:

iterate = \f a -> scanl (\a f -> f a) a (repeat f)