I am trying to write a recursive function in clojure
. The function returns the greatest number of given collection. If the collection is emty then it should return nil
.
My code is:
(defn gum [coll]
(if (empty? coll)
0
(max (first coll)
(gum (rest coll)))))
Expected result:
(gum [1 2 98 -3]) => 98
(gum [1 9]) => 9
(gum []) => nil
But I am getting:
(gum [1 2 98 -3]) => 98
(gum [1 9]) => 9
(gum []) => 0 (not desired result - should be `nil`)
This is because I have kept the value of (empty? coll)
as 0
. If I keep it as nil
then (gum [1 2 98 -3])
won't work. Any suggestion as how to bring the value of (gum [])
as nil
and (gum [1 2 98 -3])
as 98
at the same time?