Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (33 sloc) 1.19 KB
(defmacro hello [x]
(str "Hello, " x))
(defmacro infix [form]
(list (second form) (first form) (nth form 2)))
(defmacro infix-better [form]
`(~(second form) ; Note the syntax-quote (`) and unquote (~) characters!
~(first form)
~(nth form 2) ))
(defmacro r-infix [form]
(cond (not (seq? form))
form
(= 1 (count form))
`(r-infix ~(first form))
:else
(let [operator (second form)
first-arg (first form)
others (drop 2 form)]
`(~operator
(r-infix ~first-arg)
(r-infix ~others)))))
(meditations
"Macros are like functions created at compile time"
(= "Hello, Macros!" (hello "Macros!"))
"Can I haz some infix?"
(= (+ 9 1) (infix (9 + 1)))
"Remember, these are nothing but code transformations"
(= '(+ 9 1) (macroexpand '(infix (9 + 1))))
"You can do better than that, hand crafting ftw!"
(= '(* 10 2) (macroexpand '(infix-better (10 * 2))))
"Things dont always work as you would like them to... "
(= '(+ 10 (2 * 3)) (macroexpand '(infix-better ( 10 + (2 * 3)))))
"Really, you dont understand recursion until you understand recursion"
(= 36 (r-infix (10 + (2 * 3) + (4 * 5)))))