4 ms·
I have not played with Clojure since it was introduced, but I have a question about recur or tail recursion. Here is a trivial CL example (nontrivial might be
by Xurinos 17y ago
I have not played with Clojure since it was introduced, but I have a question about recur or tail recursion. Here is a trivial CL example (nontrivial might be approaches to parsing):
(defun find-nth-match (test from n)
(if (= n 0)
from
(martial-n test (1+ from) n)))
(defun martial-n (test from n)
(find-nth-match test
from
(if (funcall test from) (1- n) n)))
(find-nth-match #'evenp 1 5) => 10
The idea is that tail recursion is not just recursing into the exact same function you appear to be in. Is this available with current clojure? Can recur help me somehow?
- rarrrrrr 17y agoFor mutual recursion, you need to use trampoline. http://groups.google.com/group/clojure/browse_frm/thread/6257cbc4454bcb85/7d5fd827cd549080#7d5fd827cd549080 http://groups.google.com/group/clojure/browse_frm/thread/625...
- cgrand-net 17y agoNo recur won't help you: it's only for self tail recursion, not for mutual tail-recursive calls. You can use a trampoline or refactor your code.