0

We have iterate function in haskell Prelude

iterate :: (a -> a) -> a -> [a]
iterate f x == [x, f x, f (f x), ...]

What is the equivalent in C#?

mitaness
  • 695
  • 7
  • 13

1 Answers1

7

There isn't one but you can write your own:

public static IEnumerable<T> Iterate<T>(T seed, Func<T, T> step)
{
    while(true)
    {
        yield return seed;
        seed = step(seed);
    }
}
Lee
  • 142,018
  • 20
  • 234
  • 287