Bobbi Towers / Jun 26 2019

Clojure for the Brave and True

(defmacro backwards
  [form]
  (reverse form))

(backwards (" backwards" " am" "I" str))
"I am backwards"

An Overview of Clojure’s Evaluation Model

(def addition-list (list + 1 2))
(eval addition-list)
3
(eval (concat addition-list [10]))
13
(eval (list 'def 'lucky-number (concat addition-list [10])))
lucky-number
13

The Reader

(read-string "(+ 1 2)")
List(3) (+, 1, 2)
(list? (read-string "(+ 1 2)"))
true
(conj (read-string "(+ 1 2)") :zagglewag)
List(4) (:zagglewag, +, 1, 2)
(eval (read-string "(+ 1 2)"))
3

Reader macros

(read-string "#(+ 1 %)")
List(3) (fn*, Vector(1), List(3))
(read-string "'(a b c)")
List(2) (quote, List(3))
(read-string "@var")
List(2) (clojure.core/deref, var)
(read-string "; ignore!\n(+ 1 2)")
List(3) (+, 1, 2)

The Evaluator

(eval (list (read-string "+") 1 2))
3
(eval (read-string "()"))
List(0) ()

Macros

(eval
 (let [infix (read-string "(1 + 1)")]
   (list (second infix) (first infix) (last infix))))
2
(defmacro ignore-last-operand
  [function-call]
  (butlast function-call))

(ignore-last-operand (+ 1 2 10))
3
(ignore-last-operand (+ 1 2 (println "look at me!!!")))
3
(macroexpand '(ignore-last-operand (+ 1 2 10)))
List(3) (+, 1, 2)
(defmacro infix
  [infixed]
  (list (second infixed) 
        (first infixed) 
        (last infixed)))

(infix (1 + 2))
3