1

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?

Sami Liedes
  • 1,084
  • 8
  • 19
  • 2
    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 Jan 10 '17 at 21:45

1 Answers1

2

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

iterate = \f a -> scanl (\a f -> f a) a (repeat f)
rampion
  • 87,131
  • 49
  • 199
  • 315