macros

Send to Kindle
home » snippets » clojure » macros


From macros

(defmacro around-zero [number negative-expr zero-expr positive-expr]
  `(let [number# ~number] ; so number is only evaluated once
    (cond
      (< (Math/abs number#) 1e-15) ~zero-expr 
      (pos? number#) ~positive-expr
      true ~negative-expr)))

The back-quote (a.k.a. syntax quote) at the beginning of the macro definition prevents everything inside from being evaluated unless it is unquoted. This means the contents will appear literally in the expansion, except items preceded by a tilde (in this case, number, zero-expr, positive-expr and negative-expr). When a symbol name is preceded by a tilde inside a syntax quoted list, its value is substituted. Bindings in syntax quoted lists whose values are sequences can be preceded by ~@ to substitute their individual items.