8 ms·
This isn't a _general_ tail call optimization--just tail recursion. The issue is that this won't support mutual tail recursion. e.g.: (defun func-a (x) (func-
by 1932812267 1y ago
This isn't a _general_ tail call optimization--just tail recursion. The issue is that this won't support mutual tail recursion.
e.g.:
(defun func-a (x) (func-b (- x 34))
(defun func-b (x) (cond ((<= 0 x) x)
('t (func-a (-x 3))))
Because func-a and func-b are different (JVM) functions, you'd need an inter-procedural goto (i.e. a tail call) in order to natively implement this.
As an alternative, some implementations will use a trampoline. func-a and func-b return a _value_ which says what function to call (and what arguments) for the next step of the computation. The trampoline then calls the appropriate function. Because func-a and func-b _return_ instead of actually calling their sibling, the stack depth is always constant, and the trampoline takes care of the dispatch.
- knome 1y agoSounds like a manual form of clojures recur function. https://clojuredocs.org/clojure.core/recur https://clojuredocs.org/clojure.core/recur
- 1932812267 1y agoClojure's loop/recur is specifically tail recursion like scala's tailrec or the optimization described in the blogpost. It doesn't use trampolines to enable tail calls that aren't tail recursion.