Q:

Implementing boolean similar to built-in boolean

I am working through some Clojure tutorials and one of the problems is: Implement (boolean x), which works like the built-in boolean function: for nil and false, it returns false, and for all other values it returns true. You can use if in its implementation, but not the build-in boolean.
I cannot figure out how to do this with just the if statement. This works, but is there a better way?
(defn boolean [x]
   (cond
     (= x nil) false
     (= x false) false
     :else true))

A:

nil and false are both 'falsey' (i.e. are equivalent to false in a boolean context) so you can do:
(defn boolean [x]
  (if x true false))

if expressions are of the form (if condition true-expr false-expr) so if condition evaluates to true the entire expression evaluates to true-expr otherwise false-expr. So if x is truthy (i.e. not nil or false) the expression will evaluate to true. If x is falsey (nil and false are the only falsey values in clojure) then false is returned.

