问题描述
我冲浪进入这网站日前对匿名递归在C#。本文的主旨是,下面的code不会在C#中工作:
I surfed into this site a few days ago on "Anonymous Recursion in C#". The thrust of the article is that the following code will not work in C#:
Func<int, int> fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n;
文章接着将详细介绍如何使用钻营和的找回匿名递归在C#。这是pretty的有趣,但一点点复杂,我每天编码我很害怕。在这一点上,至少...
The article then goes into some detail about how to use currying and the Y-combinator to get back to "Anonymous Recursion" in C#. This is pretty interesting but a tad complex for my everyday coding I am afraid. At this point at least...
我喜欢看的东西为我自己,所以我开了单 CSHARP REPL 并进入该行。没有错误。所以,我进入 FIB(8);
。令我大为吃惊的,它的工作!该REPL回答回来 21
!
I like to see stuff for myself so I opened the Mono CSharp REPL and entered that line. No errors. So, I entered fib(8);
. Much to my great surprise, it worked! The REPL replied back with 21
!
我想也许这是一些神奇的REPL,所以我发射行动'六',键入下面的程序,并编译它。
I thought maybe this was some magic with the REPL, so I fired-up 'vi', typed in the following program, and compiled it.
using System;
public class Program
{
public static void Main(string[] args)
{
int x = int.Parse(args[0]);
Func<int, int> fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n;
Console.WriteLine(fib(x));
}
}
据建成并运行完美呢!
It built and ran perfectly too!
我在Mac上运行单2.10。我没有访问到Windows机器现在,所以我不能在Windows .NET平台进行测试。
I am running Mono 2.10 on a Mac. I do not have access to a Windows machine right now so I cannot test this on .NET on Windows.
这已经被固定在.NET一样好,甚至这是单声道的静音功能?这篇文章是几年的历史。
Has this been fixed on .NET as well or is this a silent feature of Mono? The article is a couple of years old.
如果它仅适用于黑白,我不能在接下来的面试等待,他们让我在我的选择(单声道C#)的语言,我必须提供.NET将无法正常工作的警告写Fibinocci功能。嗯,其实我可以等待,因为我热爱我的工作。不过,有趣的......
If it is Mono only, I cannot wait for the next job interview where they ask me to write a Fibinocci function in the language of my choice (Mono C#) where I have to provide the caveat that .NET will not work. Well, actually I can wait since I love my job. Still, interesting...
更新:
单是不是真的在做匿名递归。我的错。该单C#编译器假定一个空
值 FIB
转让前的其实是一个错误,如下所述。我说编译,因为.NET CLR将运行产生的组装就好了,即使.NET C#编译器不会编译code。
Mono is not really doing "anonymous" recursion as it is using fib
as a named delegate. My bad. The fact that the Mono C# compiler assumes a null
value for fib
before assignment is a bug as noted below. I say "compiler" because the .NET CLR would run the resulting assembly just fine even though the .NET C# compiler would not compile the code.
对于所有采访纳粹在那里:
For all the interview Nazis out there:
Func<int, int> fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n;
可以换成一个迭代版本:
can be replaced with an iterative version:
Func<int, int> fib = n =>
{
int old = 1;
int current = 1;
int next;
for (int i = 2; i < n; i++)
{
next = current + old;
old = current;
current = next;
}
return current;
};
您可能希望这样做,因为递归版本是低效的,如C#语言。有些人可能会建议使用记忆化但是,由于这仍比迭代方法较慢,他们可能只是是wankers。 : - )
You might want to do this because the recursive version is inefficient in a language like C#. Some might suggest using memoization but, since this is still slower than the iterative method, they may just be being wankers. :-)
在这一点上,虽然,这成为更多的广告功能编程比什么都重要(因为递归版本那么多漂亮)。这真的没有什么与我原来的问题,但一些答案认为这是非常重要的。
At this point though, this becomes more of an ad for functional programming than anything else (since the recursive version is so much nicer). It really does not have anything to do with my original question, but some of the answers thought that it was important.
推荐答案
这是一个错误 Mono的编译器。它违反了规范第§12.3.3。变量的 FIB 的不能在变量初始化中使用,因为它没有明确赋值。
This is a bug in the Mono compiler. It violates section §12.3.3 of the specification. The variable fib cannot be used in the variable initializer, because it isn't definitely assigned.
这篇关于请问&QUOT;匿名递归&QUOT;在.NET中工作?它在单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!