I am studying Clojure solving the problems listed in 4clojure . One exercise is to create your own function maxwith variable arguments.
I am trying to solve this simple problem using REPL and I got to this solution:
(defn my-max
[first & more] (calc-max first more))
(defn calc-max
[m x]
(cond (empty? x) m
(> (first x) m) (calc-max (first x) (rest x))
:else calc-max m (rest x)))
Which works fine, but the exercise does not allow use def, and so I have to flip both functions into one. When I replace the link calc-maxwith my code, the result is:
(defn my-max
[first & more]
((fn calc-max
[m x]
(cond (empty? x) m
(> (first x) m) (calc-max (first x) (rest x))
:else calc-max m (rest x)))
first more))
But this code does not work and returns the following error:
user=> (my-max 12 3 4 5 612 3)
java.lang.ClassCastException: java.lang.Integer cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0)
I assume that this error is due to an attempt to evaluate the result of the function calc-max, and I assume that this is a syntax error on my part, but I cannot figure out how to resolve it.