4 ms·
I believe the trampoline function / special form can do the mutual tail-call in clojure (http://clojure.org/api#toc569 http://clojure.org/api#toc569) : tramp
by jurjenh 17y ago
I believe the trampoline function / special form can do the mutual tail-call in clojure (http://clojure.org/api#toc569 http://clojure.org/api#toc569) :
trampoline can be used to convert algorithms requiring
mutual recursion without stack consumption.
but I haven't used it yet, am still fairly new to lisp / clojure, so certainly am not an expert in this field. YMMV
- sharkbrainguy 17y agoYou can use trampolines in dumber languages too, so that mutual recursion without stack consumption is possible in for e.g. Javascript. http://pastebin.com/m73e36bfc http://pastebin.com/m73e36bfc
- btilly 17y agoGood point. That is possible, albeit with a modest overhead. The overhead would be irrelevant except that state machines get used in very performance critical ways, such as inside a regular expression engine. But possibly Clojure isn't the right language if you care about performance that much. Incidentally I'd be shocked if trampoline was a special form rather than a function. I'm not sure what type a function is (I don't use clojure nor is it installed here), but it should be possible to implement it something like this: (defn trampoline [f] (if (eq? (type f) 'function') (recur (f)) f))
- spuz 17y agoIndeed, Clojure's trampoline is a function: (defn trampoline ([f] (let [ret (f)] (if (fn? ret) (recur ret) ret))) ([f & args] (trampoline #(apply f args))))