logoalt Hacker News

ndrtoday at 11:49 AM1 replyview on HN

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


Replies

pfdietztoday at 12:29 PM

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.