News for hackers. Stuff that matters.
Re:SICP in Clojure (In Progress) by dschobel on Saturday October 24, 2009 @05:55PM (#900979)
Kind of off-topic, but I'm pretty surprised clojure doesn't do TCO.

Is that a JVM limitation?

Re:SICP in Clojure (In Progress) by ikitat on Saturday October 24, 2009 @06:00PM (#900982)
Yes
Re:SICP in Clojure (In Progress) by dantheman on Saturday October 24, 2009 @06:39PM (#901006)
This one of the things that Microsoft got right with the CLR. That TCO is required by scheme is where I finally grokked programming language semantics -- how implementation and form are intertwined.
Re:SICP in Clojure (In Progress) by tl on Saturday October 24, 2009 @07:48PM (#901050)
Isn't the TCO problem largely a non-issue? My understanding is that if you do (defn function [...] ... (recur ...)) instead of (defn function [...] ... (function ...)) the compiler handles it for you.
Re:SICP in Clojure (In Progress) by scythe on Saturday October 24, 2009 @07:56PM (#901057)
There's more to tail-call optimization than simply imitating iteration using recursion.
Re:SICP in Clojure (In Progress) by dmoney on Saturday October 24, 2009 @08:03PM (#901062)
What else is it used for?
Re:SICP in Clojure (In Progress) by silentbicycle on Sunday October 25, 2009 @12:04PM (#901692)
It may help to stop thinking of it being used specifically for recursion - there can be tail calls between several functions. In cases where TCO is applicable, function A calls B, and B calls C, but since B isn't doing anything to the result, C can return directly to A. This is transitive - D, E, F, ... could all return to A as well, in constant space. It "goto"s the next function (but with arguments), and returning and popping the results off the call stack is deferred.

Rather than going into fairly abstract examples* , TCO is useful because it allows more things to be expressed in terms of function calls, without having to worry about blowing the stack. While the discussion surrounding them can be a bit overly academic, they ultimately matter because they can help make code clearer. (Not always, but you can implement clearer looping mechanisms on top of them.) A tail call is like a de-fanged goto.

* Just one: Parsing by implementing a finite state machine in function calls. See Shriram Krishnamurthi's "The Swine Before Perl" (http://www.cs.brown.edu/~sk/Publications/Talks/SwineBeforePe...).

While the stacktrace no longer records intermediate steps, (and thus loses info useful for debugging), language implementations can typically hook tail calls to save the information when a debugging flag is on. It's not as big a problem in practice as some people think.

Re:SICP in Clojure (In Progress) by chancho on Saturday October 24, 2009 @08:23PM (#901087)
Re:SICP in Clojure (In Progress) by Xichekolas on Sunday October 25, 2009 @12:08AM (#901237)
Also, this blog post (which did the rounds on HN a while back) was excellent for understanding it:

http://funcall.blogspot.com/2009/04/you-knew-id-say-somethin...

(It's part of a 5 part series... but you don't have to necessarily read all of them... it kind of wanders.)