-3

I want to find Haskell's analogue of infinite list in c# language. I want to use any possible interfaces for such list, like these:

IEnumerable

ICollection

IList

| improve this question | | | | |
  • 2
    Given the yield keyword in C#, most collection types can be thought to be able to return an infinite list (depending on actual implementation, of course). – Oded Sep 13 '16 at 14:45
  • @Oded, this approach can only implement IEnumerable interface. For example, I want to use indexer method for such list. IEnumerable allows only to use foreach semantics – LmTinyToon Sep 13 '16 at 14:46
  • Then write an indexer, and skip items until you get to the desired index. – Robert Harvey Sep 13 '16 at 14:47
  • 1
    IEnumerable<T> works perfectly fine for infinite sequences. ICollection<T> has a Count property which makes no sense for infinite sequences, so implementing ICollection<T> and IList<T> are rather dubious for infinite collections. – CodesInChaos Sep 13 '16 at 20:11
  • 1
    Perhaps you should rewrite your question to be what you actually want, instead of writing a different question then complaining when the answerers failed to read your mind. – whatsisname Sep 14 '16 at 2:57
1

I found an article on the subject here.

Essentially you can utilize the yield keyword to programmatically create the next element of your list.

| improve this answer | | | | |
  • Thanks, but such appropach has too narrow possibilities (Note: author uses only IEnumerable interface). Simple Example: I want to use concatenation of simple list and infinite list – LmTinyToon Sep 13 '16 at 15:27
  • I found useful moment in Article (using Linq laziness). Maybe it will be answer – LmTinyToon Sep 13 '16 at 15:31
2

Using IEnumerable and yield return you can easily create infinite sets.

static IEnumerable<uint> UpDown(uint min = uint.MinValue, uint max = uint.MaxValue, uint step = 1)
{
    if (min > max)
        throw new ArgumentOutOfRangeException("min", "'min' must be less than or equal to 'max'");
    var current = min;
    var last = current;
    var up = true;
    while (true)
    {
        yield return current;
        current += (uint)(up ? step : -step);
        if (up && (current >= max || current < last))
        {
            up = !up;
            current = max;
        }
        else if (!up && (current <= min || current > last))
        {
            up = !up;
            current = min;
        }
        last = current;
    }
}

static IEnumerable<int> Random()
{
    while (true)
        yield return 4;
}
| improve this answer | | | | |
1

The type that most closely corresponds to a lazy Haskell-like list, is IEnumerable<out T>.

| improve this answer | | | | |
  • Thanks, but such appropach has too narrow possibilities. Simple Example: I want to use concatenation of simple list and infinite list – LmTinyToon Sep 13 '16 at 15:26
  • 3
    @АлександрЛысенко - You can concat a simple list and an infinite list in C# with Linq Concat function, where both are represented by IEnumerable<T> – antlersoft Sep 13 '16 at 15:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.