I am trying to do a comparison to find the largest number in a list. I know I could just use the (max 1 2 4 3) but I'm trying to write the function myself. However, I'm not too sure where the error is. Whenever I run this, the maximum is always 2. Am I doing a comparison wrong somewhere or forget something?
(defn maxVal [list] (def maxValue 0) (doseq [x list] (println x maxValue) (if > x maxValue) (do (println x ">" maxValue) (def maxValue x))) (var-get #'maxValue)) (maxVal '(1 4 3 2))
The problem is that your code is evaluating
> as a value, not invoking it as a function.
(if > x maxValue)
> is neither
nil nor false. Consequently, the current indentation (while it reflects intent accurately) is a bit misleading; the
do should be outside the
if, since it takes place no matter what happened prior.
Presumably, what you instead want is:
(if (> x maxValue)