In Clojure you can write:
(cond (= 1 2) 1
:else 5)
In Scheme the equivalent would be:
(cond ((= 1 2) 1)
(else 5))
The :else 5 syntax is not as consistent as the (else 5). What is the reason the else syntax is implemented in this seemingly inconsistent way in Clojure?
:else is a actually a bit of a clever trick here:
cond expects condition/value pairs - and :else is just a value that is "truthy" in Clojure so it guarantees that the condition is satisfied. (anything except null or false counts as "truth"). You could equally use ":donkey" as a guaranteed true condition value if you liked.So really it's just a convention that works in cond expressions and is meaningful to human readers.
I think (else 5) is less consistent. (cond ...) arguments are stated as condition - value pairs. :else value is consistent because :else is just a convention - it works because :else is just an expression that's always true. There's no special rules for :else at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With