Sto cercando di implementare una macro per convertire in modo ricorsivo una lista di infissi in una prefisso. Incontro un problema come segue:In clojure, come eseguire la modellazione del codice durante l'implementazione di una macro mediante ricorsione
;;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]
Dove è il problema? Come definire correttamente una macro con il codice dei template?
P.S. Ho cambiato il codice in questo e funziona, perché? qual è la differenza ?:
(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)))))
ha qualcosa a che fare con la messa "blocco if" nella gamma vincolante di backquote? – lkahtz