I’ve been learning Clojure, and since I come from a Ruby, and before that Java background, I have trouble thinking procedurally.
Is there a more ‘lispy’ way to write this code, or is this ok?
(defn foo
([s t]
(let [x (+ 4 (- t s))]
(if (> 2 (if (> 6 x)
x
6)
x)
x
2))))
In clojure, like in any other language, it is usually best to use built-in functions whenever applicable. So since clojure has a
minand amaxfunction, so you can replace yourifs with:If those functions did not exist in clojure’s standard library, I would have recommended defining them because putting the logic for
minandmaxinto their own function leads to much nicer code than having it all in thefoofunction.