Clojure tail call recursion with variable argument function

I basically want this:

(defn mymax ([ab] (if (> ab) ab)) ([ab & rest] (apply recur (conj rest (mymax ab))))) 

So: (mymax 1 2 3 4) tail calls (mymax 2 3 4) , which calls tail (mymax 3 4)

I see a problem in which "apply" stops the repetition in the tail position, which means that it will not work. But I do not see how I can not use the application for variable function arguments

[Please note, I know that you can solve this specific problem with reduction. Just wondering if you can do tail-call-recursion with params variables]

+4
source share
1 answer

Make the function take a single vector as an argument, and not use aruguments as a sequence of values. This will allow you to get rid of the application.

 (defn mymax [[ab & rest]] (let [m (if (> ab) ab)] (if (not rest) m (recur (conj rest m))))) 
+7
source

Source: https://habr.com/ru/post/1389869/


All Articles