'Why is there no tail recursion optimization in Emacs lisp, not but like other scheme?

Emacs lisp is a dialect of LISP and especially Scheme. Most of scheme interpreters do have a optimization of Tail Recursion, but emacs lisp doens't. I searched the reason in `info elisp' for a while, but I fail to find it.

P.S. Yes, there is other iteration syntax in elisp like `while', but I still cannot find a good reason why they didn't implement tail recursion like other scheme interpreters.



Solution 1:[1]

Emacs Lisp was created in the 1980's. The Lisp dialect that the Emacs author (Richard Stallman) was most familiar with at the time was MIT Maclisp, and Emacs Lisp is very similar to it. It used dynamic variable scoping and did not have lexical closures or tail recursion optimization, and Emacs Lisp is the same.

Emacs Lisp is very different from Scheme. The biggest difference is that Scheme is lexically scoped, but this was only recently added to Emacs Lisp, and it has to be enabled on a per-file basis (by putting ;;; -*- lexical-binding: t -*- on the first line) because doing it by default would cause many incompatibilities.

There has been some work to replace Emacs Lisp with Guile, a Scheme dialect. But it's not clear whether it will ever reach fruition.

Solution 2:[2]

Emacs Lisp has had dynamic scoping as its main/only scoping rule for the first 25 years of its life. Dynamic scoping is basically incompatible with optimization of tail-recursion, so until Emacs-24 (which introduced lexical scoping) there was very little to no interest in this optimization.

Nowadays, ELisp could benefit sometimes from optimization of tail recursion, and there's been some patches submitted to do that, but that hasn't yet been integrated. The lack of tail-recursion optimization as well as the relatively inefficient implementation of function calls has influenced ELisp style, such that recursion is not used very often, which in turns reduces the benefits of adding the optimization of tail calls.

Solution 3:[3]

Looks like someone has made an implementation of TCO in Emacs Lisp: https://github.com/Wilfred/tco.el. I haven't played with it myself, but you might want to give it a whirl if you're interested in seeing TCO in Emacs Lisp.

Solution 4:[4]

Emacs 28 introduced the macro named-let, which can be used to evaluate a tail-recursive loop expression in an optimized way.

While there's no direct support for auto-optimizing functions yet, we can use the above macro inside those functions; or if you are adventurous, set native-comp-speed to 3 (also Emacs 28+): Self TCO by GCCEmacs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Stefan
Solution 3 GDP2
Solution 4 Daanturo