I have this macro (from the clojure-koans) that should allow the use of infix operators:
(defmacro infix-better [form]
`(~(second form)
(first form)
(last form) ))
It does what it is supposed to, but doesn’t exactly expand to the same expression. For example:
user=> (= '(* 10 2) (macroexpand '(infix-better (10 * 2))))
false
user=> '(* 10 2)
(* 10 2)
user=> (macroexpand '(infix-better (10 * 2)))
(* (clojure.core/first user/form) (clojure.core/last user/form))
The last output would end up being (* 10 2) when the inner expressions are evaluated but the equality test returns false since (clojure.core/first user/form), strictly speaking, isn’t 10.
How can I have the expanded macro equal the equivalent hard-coded Clojure?
You have to unquote the second and third forms, just like you did the first, with
~– “The last output would end up being (* 10 2) when the inner expressions are evaluated” is not in any way true – the inner expressions have already passed their chance to be evaluated.Would fix it and keep your code structured the same, but really destructuring gives you a much nicer result: