Question

i have a piece of code like this

class Program
{
    static IEnumerable<string> GetSequences(string a)
    {
        yield return a;
        GetSequences(a + ">");
    }

    static void Main(string[] args)
    {
        foreach (var n in GetSequences(">"))
            Console.Write(n + ",");
    }
}

i was expecting an output like this

,>>,>>>

but it doesn't. it printed only ">,". Does anyone knows what i am missing ?

Was it helpful?

Solution 2

The foreach loop only works with the yield return, and you don't have a yield return on your GetSequences() command in the GetSequences() method; whatever it returns doesn't get stored or returned. It is like you are doing this: It's like you are doing this:

static IEnumerable<string> GetSequences(string a)
{
    GetSequences(a + ">");
}

which of course doesn't have a return statement (it wouldn't compile, but you knew that). After toying around with this for a little while, if you want to use recursion I would recommend you don't use an enumerable, particularly because loops and recursion are meant to serve separate purposes, and the foreach with IEnumerable works best with collections that have already been enumerated. The loop suggested above, as is, just enables endless recursion, and implementing an escape of the recursion like this:

    static IEnumerable<string> GetSequences(string a)
    {
        if(a.Length > 100)
            yield return a;
        else
            foreach (var n in GetSequences(a + ">"))
                yield return n;            
    }

yields this output: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>, (maybe to scale, I didn't want to count) By implementing it like this I was able to get the output you wanted:

    static string GetSequences(string a, int len)
    {
        if (a.Length < len)
        {
             return GetSequences(a + ">", len);
        }
        else
            return a;
    }

    static void Main(string[] args)
    {

        for (int i = 1; i < 5; i++)
        {
            Console.Write(GetSequences(">", i) + ",");
        }
        Console.Read();
    }

Of course, my integers are arbitrary, it will work with any length.

EDIT: I knew there was a way to do it the way abatishchev was saying, but I couldn't figure it out. After racking my brain, here is what I got:

    static IEnumerable<string> GetSequences(string a)
    {
        if (a.Length < 100)
        {
            yield return a;
            foreach (var n in GetSequences(a + ">"))
                yield return n;
        }else
            yield break;
    }

This has the output you want, although I still think using recursion with loops is a little funny.

OTHER TIPS

Use the same foreach in function itself:

static IEnumerable<string> GetSequences(string a)
{
    yield return a;
    foreach (var n in GetSequences(a + ">"))
        yield return n;
}

and don't forget to quit recursion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top