Logical truthness in Clojure -


what true? says actually? (true? 0)=false. (if 0 "0 true" "0 false")=0 true. why happens?

you confusing 2 things:

  • the different values there in clojure, and
  • the way if , progeny treat these values.

true , 1 values, , different:

(= true 1) ; false 

but have same effect first arguments if:

(if true "hello!" "goodbye.") ; "hello!"  (if 1 "hello!" "goodbye.") ; "hello!" 

in fact, first argument causes if evaluate , return second argument:

(if + "hello!" "goodbye.") ; "hello!"  (if *ns* "hello!" "goodbye.") ; "hello!"  (if string "hello!" "goodbye.") ; "hello!" 

there 2 values cause if evaluate , return third argument. 2 values false , nil.

(if false "hello!" "goodbye.") ; "goodbye."  (if nil "hello!" "goodbye.") ; "goodbye." 

if no third argument supplied, defaults nil:

(if false "hello!") ; nil 

the same distinction between values applies other clojure conditionals, - directly or indirectly - derived if: if-not, when, when-not, and, or, &c. these expressed macros, that, if, not evaluate arguments until need to.

to quote official documentation

(if test else?)

evaluates test. if not singular values nil or false, evaluates , yields then, otherwise, evaluates , yields else. if else not supplied defaults nil. of other conditionals in clojure based upon same logic, is, nil , false constitute logical falsity, , else constitutes logical truth, , meanings apply throughout.


Comments