I did a bit more work with Clojure today.
My imperative programming habits are still bleeding through.
The exercise introduced cond
as a sort of case statement for flow control.
I wrote a simple cond
statement but was getting a bizarre runtime error:
(defn my-fn
[x]
(cond
(x < 0) "negative"
(x = 0) "zero"
(x > 0) "positive"
)
)
user=> (my-fn 1)
Execution error (ClassCastException) at user/my-fn (REPL:4).
class java.lang.Long cannot be cast to class clojure.lang.IFn (java.lang.Long is in module java.base of loader 'bootstrap'; clojure.lang.IFn is in unnamed module of loader 'app')
It took me a frustratingly long time to realize I needed to use prefix notation for the conditions in the cond
.
(defn my-fn
[x]
(cond
(< x 0) "negative"
(= x 0) "zero"
(> x 0) "positive"
)
)
user=> (my-fn 1)
"positive"