
Infinite Recursion - luccastera
http://paulbarry.com/articles/2009/09/02/infinite-recursion
======
Periodic
This had me laughing out loud:

"The same mistake is done on the Computer Language Benchmark Game. There's an
Array sorting benchmark where Haskell just blows away even hand-optimized C.
The secret is that the benchmark never prints out the contents of the sorted
array. The C compiler isn't smart enough to recognize that the array is never
actually used anywhere, but Haskell is, and so the entire benchmark basically
compiles down to the equivalent of "int main() { return 0; }"."

------
yangyang
He forgot C! It's not in the language spec, but gcc will optimize tail calls,
with -O2 or above.

------
amichail
From: [http://stronglytypedblog.blogspot.com/2009/08/scala-tail-
rec...](http://stronglytypedblog.blogspot.com/2009/08/scala-tail-
recursion.html)

 _If the recursion is indirect, for example, Scala cannot optimize tail calls,
because of the limited JVM instruction set._

Why is that?

~~~
jdale27
Apparently the JVM goto instruction can only jump to an address within the
same method:

[http://java.sun.com/docs/books/jvms/second_edition/html/Inst...](http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc5.html)

~~~
bigmac
I think the CLR team was able to learn from some of the JVM's shortcomings;
this limitation is one example. .NET IL has the TAIL. instruction prefix that
allows tail calls. It can be prepended to any call instruction outside of an
exception handling block.

There's also the JMP instruction, which is similar and allows the current
method's arguments to be passed to another function, discarding the current
stack frame and returning to the current method's caller.

~~~
flatline
Even more interesting is that C# and VB.NET, the two main languages targeting
the CLR, do not make use of it. Before posting this though, I did a quick
search to make sure they hadn't turned around and implemented it for C# 4 or
something, and found this link:

[https://connect.microsoft.com/VisualStudio/feedback/ViewFeed...](https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=166013&wa=wsignin1.0)

Apparently there are some performance issues with the TAIL IL instruction,
though I wonder if it's really as slow as pushing a new frame on the call
stack.

