Focus the problem and clean up naming in macros

Fixes #134
master
Colin Jones 6 years ago
parent faa02b5fae
commit 809af93e35
No known key found for this signature in database
GPG Key ID: BB1DBD3616F1DDB9

@ -264,8 +264,6 @@
(drop 2 form) (drop 2 form)
"Hello, Macros!" "Hello, Macros!"
10 10
'(+ 9 1) '(+ 9 1)]}]
'(* 10 2)
'(+ 10 (2 * 3))]}]
] ]

@ -7,23 +7,23 @@
(defmacro infix [form] (defmacro infix [form]
(list (second form) (first form) (nth form 2))) (list (second form) (first form) (nth form 2)))
(defmacro infix-better [form] (defmacro infix-concise [form]
`(~(second form) ; Note the syntax-quote (`) and unquote (~) characters! `(~(second form) ; Note the syntax-quote (`) and unquote (~) characters!
__ __
__ )) __))
(defmacro r-infix [form] (defmacro recursive-infix [form]
(cond (not (seq? form)) (cond (not (seq? form))
__ __
(= 1 (count form)) (= 1 (count form))
`(r-infix ~(first form)) `(recursive-infix ~(first form))
:else :else
(let [operator (second form) (let [operator (second form)
first-arg (first form) first-arg (first form)
others __] others __]
`(~operator `(~operator
(r-infix ~first-arg) (recursive-infix ~first-arg)
(r-infix ~others))))) (recursive-infix ~others)))))
(meditations (meditations
"Macros are like functions created at compile time" "Macros are like functions created at compile time"
@ -36,10 +36,10 @@
(= __ (macroexpand '(infix (9 + 1)))) (= __ (macroexpand '(infix (9 + 1))))
"You can do better than that - hand crafting FTW!" "You can do better than that - hand crafting FTW!"
(= __ (macroexpand '(infix-better (10 * 2)))) (= '(* 10 2) (macroexpand '(infix-concise (10 * 2))))
"Things don't always work as you would like them to... " "Things don't always work as you would like them to... "
(= __ (macroexpand '(infix-better ( 10 + (2 * 3))))) (= '(+ 10 (2 * 3)) (macroexpand '(infix-concise (10 + (2 * 3)))))
"Really, you don't understand recursion until you understand recursion" "Really, you don't understand recursion until you understand recursion"
(= 36 (r-infix (10 + (2 * 3) + (4 * 5))))) (= 36 (recursive-infix (10 + (2 * 3) + (4 * 5)))))

Loading…
Cancel
Save