I am trying to implement a macro to recursively converting an infix list into a prefix one. I encounter a problem as follows:
;;this works
(defmacro recursive-infix [form]
(list (second form) (first form)
(if (not (seq? (nth form 2)))
(nth form 2)
(recursive-infix (nth form 2)))))
;;this doesn't work
(defmacro my-recursive-infix [form]
`(~(second form) ~(first form)
(if (not (seq? ~(nth form 2)))
~(nth form 2)
(my-recursive-infix ~(nth form 2)))))
(macroexpand '(recursive-infix (10 + 10)))
;;get (+ 10 10)
(macroexpand '(my-recursive-infix (10 + 10)))
;;get (+ 10 (if (clojure.core/not (clojure.core/seq? 10)) 10 (user/my-recursive-infix 10)))
(recursive-infix (10 + 10))
;;get 20
(my-recursive-infix (10 + 10))
;;Don't know how to create ISeq from: java.lang.Integer [Thrown class java.lang.IllegalArgumentException]
Where is the problem? How to correctly define a macro with code templating?
P.S. I changed the code into this and it works, why? what is the difference?:
(defmacro my-recursive-infix [form]
(if (not (seq? (nth form 2)))
`(~(second form) ~(first form) ~(nth form 2))
`(~(second form) ~(first form) (my-recursive-infix (nth form 2)))))