philipbohun.com

Glojure by Example: Conditionals

if takes a test, a then-expression, and an else-expression. Each branch is one expression. Use do when a branch needs several.

(defn classify [n]
  (if (neg? n)
    "negative"
    (if (zero? n)
      "zero"
      "positive")))

(println (classify -3))
(println (classify 0))
(println (classify 4))

when runs its body only if the test is true, and returns nil otherwise. if-let binds a name when the test value is truthy.

(when (pos? 4)
  (println "four is positive"))

(if-let [name (:name {:name "Ada"})]
  (println "hello" name)
  (println "anonymous"))

Each call prints one classification.

$ glj conditionals.glj
negative
zero
positive
four is positive
hello Ada

Next example: Cond and Case.