2011-02-08 11:51:11 +00:00
|
|
|
(defmacro hello [x]
|
|
|
|
(str "Hello, " x))
|
|
|
|
|
|
|
|
(defmacro infix [form]
|
|
|
|
(list (second form) (first form) (nth form 2)))
|
|
|
|
|
|
|
|
(defmacro infix-better [form]
|
2011-02-08 13:51:41 +00:00
|
|
|
`(~(second form) ; Note the syntax-quote (`) and unquote (~) characters!
|
2011-02-08 11:51:11 +00:00
|
|
|
__
|
|
|
|
__ ))
|
|
|
|
|
|
|
|
(defmacro r-infix [form]
|
2011-02-08 13:46:48 +00:00
|
|
|
(cond (not (seq? form))
|
2011-02-08 13:51:41 +00:00
|
|
|
__
|
2011-02-08 13:46:48 +00:00
|
|
|
(= 1 (count form))
|
|
|
|
`(r-infix ~(first form))
|
|
|
|
:else
|
|
|
|
(let [operator (second form)
|
|
|
|
first-arg (first form)
|
2011-02-08 13:51:41 +00:00
|
|
|
others __]
|
2011-02-08 13:46:48 +00:00
|
|
|
`(~operator
|
|
|
|
(r-infix ~first-arg)
|
|
|
|
(r-infix ~others)))))
|
2011-02-08 11:51:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
(meditations
|
2011-02-08 13:20:38 +00:00
|
|
|
"Macros are like functions created at compile time"
|
|
|
|
(= __ (hello "Macros!"))
|
|
|
|
|
2011-02-08 11:51:11 +00:00
|
|
|
"Can I haz some infix?"
|
|
|
|
(= __ (infix (9 + 1)))
|
2011-02-08 13:20:38 +00:00
|
|
|
|
|
|
|
"Remember, these are nothing but code transformations"
|
2011-02-08 11:51:11 +00:00
|
|
|
(= __ (macroexpand '(infix (9 + 1))))
|
|
|
|
|
2011-02-08 13:20:38 +00:00
|
|
|
"You can do better than that, hand crafting ftw!"
|
2011-02-08 11:51:11 +00:00
|
|
|
(= __ (macroexpand '(infix-better (10 * 2))))
|
2011-02-08 13:20:38 +00:00
|
|
|
|
2011-02-08 11:51:11 +00:00
|
|
|
"Things dont always work as you would like them to... "
|
|
|
|
(= __ (macroexpand '(infix-better ( 10 + (2 * 3)))))
|
|
|
|
|
|
|
|
"Really, you dont understand recursion until you understand recursion"
|
2011-02-08 13:51:41 +00:00
|
|
|
(= 36 (r-infix (10 + (2 * 3) + (4 * 5)))))
|
2011-02-08 11:51:11 +00:00
|
|
|
|