Comment by phplovesong

11 hours ago

TCO can be implemented easily in non TC optimized langauges with a trampoline wrapper.

Why do i need a fully fledged library for something that is basically a few lines of code?

There's quite a bit of overhead.

I believe Clojure does it with trampoline as JVM does not (as far as I know) does not support tail call optimization. Ironic, given Guy Steele.

  • Yes, Clojure doesn't have TCO either.

    You get `(loop ... (recur ...))` or `trampoline`.

    Naive recursion will consume the stack.

    https://clojuredocs.org/clojure.core/loop

    https://clojuredocs.org/clojure.core/recur

    https://clojuredocs.org/clojure.core/trampoline

    • Nor can you count on Common Lisp to have TCO. People who are new to CL and treat it like Scheme run into this constantly. Basically never recur down a list, since the list could be long.

      This problem also shows up in cases where TCO is not possible. For example, suppose we have a syntax tree for some program and need to traverse the tree. Nodes that represent lists of things might have a long list of children, and in a sufficiently large program recursing down that list can blow out the stack. Just recurse on list elements, which one can reasonably assume don't nest too deeply.