相关疑难解决方法(0)

为什么.NET/C#不能优化尾调用递归?

我发现这个问题关于哪些语言优化尾递归.为什么C#不会优化尾递归?

对于具体情况,为什么不将此方法优化为循环(Visual Studio 2008 32位,如果这很重要)?:

private static void Foo(int i)
{
    if (i == 1000000)
        return;

    if (i % 100 == 0)
        Console.WriteLine(i);

    Foo(i+1);
}
Run Code Online (Sandbox Code Playgroud)

.net c# optimization tail-recursion

102
推荐指数
4
解决办法
3万
查看次数

如何编写此方法以使其符合尾递归优化的条件?

有人知道一个算法来对尾部进行简单的递归吗?更具体地说,您将如何将算法应用于以下代码?

namespace Testing
{
    class Program
    {
        static void Main(string[] args)
        {
           Console.WriteLine(match("?**", "aaa"));
           Console.WriteLine(match("*#*?", "aa1$a1a1"));
           Console.WriteLine(match("*#*", "aa11"));
           Console.WriteLine(match("??*", "0110"));
           Console.WriteLine(match("", "abc"));
           Console.WriteLine(match("???", ""));
           Console.ReadLine();

        }


        public static bool match(string p, string s)
        {
            if (p.Length == 0)
                return true;
            if (p.Length > s.Length)
                return false;

            bool firstLetterMatches = false;
            char nextCharInStr = s[0];
            switch (p[0])
            {
                case '*':
                    firstLetterMatches = 'a'<= nextCharInStr && nextCharInStr <= 'z';
                    break;

                case '#':
                    firstLetterMatches = '0'<= nextCharInStr && nextCharInStr <= '9';
                    break;

                case …
Run Code Online (Sandbox Code Playgroud)

c# tail-recursion

1
推荐指数
1
解决办法
356
查看次数

标签 统计

c# ×2

tail-recursion ×2

.net ×1

optimization ×1