3 ms·
It's an optimization. Most optimizations don't require that the programmer annotate "I want this optimization" around the code that can be optimized. Obviousl
by jrockway 17y ago
It's an optimization. Most optimizations don't require that the programmer annotate "I want this optimization" around the code that can be optimized. Obviously, he always wants the optimization. Tail call removal does this; it's a case that is easy for the compiler to identify and optimize. Requiring an explicit "hey, optimize this!" instruction is just confusing...
- jfager 17y agoHow is 'structure your code to make tail calls' any different from 'annotate "I want this optimization"'?
- jrockway 17y agoPresumably tail-recursion is the natural expression of your algorithm. (I will admit that I wish the compiler could optimize something of the form "sum (x:xs) = x + sum xs"; instead you have to write "sum k (x:xs) = sum (k + x) xs".)
- sethg 17y agoI don't think Clojure's "recur" or Scheme's tail-call elimination should really be called optimizations. The point of optimizations, AIUI, is that the code will still run with the optimizations off--it will just run more slowly, but might be useful in other ways (easier debugging, faster compilation, a chance to confirm that there are no bugs in the compiler/interpreter itself, whatever). But if I'm using "recur" or tail-call elimination to write loops in a recursive form, and I know that my code has to still work even if those features are turned off, then all of a sudden I have to worry about things like "how many times can I recurse in this function without exhausting the stack?"
- jrockway 17y agoProgramming for a fixed amount of memory/stack is also an optimization.