There are two camps of programmers:
1) Ones who are used to writing such algorithms (e.g. in Lisp) who cry bloody murder because they can't use them in Python. It's painful to lose that type of expressiveness.
2) Ones who have never used them, and don't realize what they're missing. These will usually complain about surface issues (like nicer stack traces for debugging).
Camps like camp #2 comes up a lot in history.
- In the eighties and nineties, most BASIC programmers had never used pointers, references, or real data structures, and saw them as abstract theoretical constructs without any real-world use.
- C / C++ programmers would call Java programmers lazy for wanting garbage collection (ignorant of the broad set of algorithms enabled if you don't need to keep track of when memory is no longer needed manually).
- Java / C / C++ programmers saw closures and a lot of other functional tools as pointless, complex, theoretical abstractions, until Python / JavaScript / etc. ate their lunch.
- Many people who have never used relational databases before see them as pointless complexity for most systems, aside from very specific use-cases.
... and so on. The other argument always made hinges on the Turing Completeness of languages ("I'd like to do X!" "Well, here's how you do it without Y. If you were just more clever, you'd see why Y is useless complexity.") You can ALWAYS implement any use case in any Turing-complete language. BASIC programmers stuck things into arrays. Java/C++ programmers could use objects instead of a closure. And tail-recursive code can always be translated into iteration.
I find it much easier to reason about loops than to reason about recursion. Likewise I find it much easier to write loops than to write recursive functions.
It probably comes down to experience mainly. I spent some time with functional languages, but I have spent far far far more time writing imperative procedural code.
The language would have to change for you to be able to code a loop. And that would have all sorts of consequences: in Erlang, preemption happens at function calls and no loops means a bounded time until you make a function call or terminate, with loops, that changes and preemption gets more difficult.
I didn't think this was true. The secret sauce was that Erlang preempted on reduction counts, so as a result nothing has any responsibility to yield itself.
edit: ah, literally no difference between the two in the implementation.
If you take out enough simplifying implementation details, you no longer have a powerful system built from simple parts.
Instead, there's loop/recur where the recur call won't compile except in tail position.
Haskell: https://hackage.haskell.org/package/recursion-schemes
Looking at TCO in isolation is missing the big picture. You need to consider other language features that uses TCO.
In Scheme/Racket the specification says that implementations must support an "unbounded number of active tail calls" (for implementations that use stack frames to implement function calls, this means that TCO must be supported).
This requirement is a global requirement.
The other feature of Scheme/Racket that compelled the authors of the Scheme standard to require an "unbounded number of active tail calls" are macros. Macros in Scheme allow users of the language to add new bindings constructs (e.g. pattern matching), new control constructs etc to the language using syntax rewrite rules. In Scheme these rewrite rules work locally. [Racket allows for global program rewrites too].
There is a range of constructs that are possible to specify locally - if tail calls don't allocate that is. As an example: There are embeddings of Prolog in Scheme.
If Scheme didn't support TCO then a global transformation of the program is needed. So instead of writing local macros - a full compiler is needed.
These days it is common to see JavaScript as a compilation target. Babel compiles JavaScript+NewFeatures to GoodOldJavaScript. The features that allow local rewriting are much easier to implement than the ones that require a global transformation. If JavaScript had TCO it would be much easier to use JavaScript as a compilation target.
This can combine with other language features and idioms, to get effects such as (in addition to what soegaard mentioned) reducing mutations, and avoiding `return` exits. Which can increase the readability/maintainability -- and therefore correctness -- of the code.
So some suggest that Tail Call Optimization for Scheme should instead be called Proper Implementation of Tail Calls.
Nah, it's just very easy to understand while being of technical nature. People tend to feel smart when they grasp it and then they can't shut up about it. They even write musicals...
For functional programming it is a required optimization for performant programs.
This is because loops don't exist in functional programming. The base way of imitating looping in functional programming is recursion then building abstractions on top of the recursion like map and reduce.
It seems that this particular optimization technique is frequently discussed and written about, leading me to wonder if it holds a unique level of importance among the other strategies.
Is my perception incorrect, or is there more to the significance of tail call optimization that I am unaware of?