I am trying to write a custom max function in Clojure, which should support one or more arguments. However, I am running into an error that I cannot figure out. Here is the below function:
(defn my-max [arg & rest]
(loop [m arg c rest]
(cond (empty? c) m
(> m (first c)) (recur m (rest c))
:else (recur (first c) (rest c)))))
And I encounter the following error when attempting to evaluate the function:
user> (my-max 2 3 1 4 5)
ClassCastException clojure.lang.ArraySeq cannot be cast to clojure.lang.IFn user/my-max (NO_SOURCE_FILE:5)
I thought this would work because I was under the assumption that rest was just a sequence. I was able to get this function to work without a variadic signature, where the argument is simply a sequence:
(defn my-max [coll]
(loop [m (first coll) c (rest coll)]
(cond (empty? c) m
(> m (first c)) (recur m (rest c))
:else (recur (first c) (rest c)))))