У меня есть следующий код:
public static IEnumerable<T> cons<T>(T y, IEnumerable<T> xs)
{
yield return y;
foreach (var x in xs) yield return x;
}
public static bool empty<T>(IEnumerable<T> xs)
{
return !xs.GetEnumerator().MoveNext();
}
public static T head<T>(IEnumerable<T> xs)
{
Debug.Assert(!empty(xs), "Prelude.head: empty list");
var e = xs.GetEnumerator(); e.MoveNext();
return e.Current;
}
// repeat x is an infinite list, with x the value of every element
public static IEnumerable<T> repeat<T>(T x)
{
return cons(x, repeat(x));
}
Почему head(repeat(2))
не работает, но если я заменю реализацию repeat
на:
// repeat x is an infinite list, with x the value of every element
public static IEnumerable<T> repeat<T>(T x)
{
for(;;) yield return x;
}
это работает?