我正在尝试实现一个宏来递归地将中缀列表转换为前缀列表。我遇到如下问题:
;;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]
问题出在哪里?如何使用代码模板正确定义宏?
P.S。我把代码改成了这个,它有效,为什么?有什么区别?:
(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)))))
答案 0 :(得分:13)
在原始版本中,(if (not ...))
检查是在编译时发生的;你把它包含在扩展代码中。因此,这是一个微小的变化,可以让它像你想要的那样行动 - 它实际上和原作一样,但是“翻转”引用的内容和不引用的内容。
(defmacro my-recursive-infix [form]
(let [third (nth form 2)]
`(~(second form) ~(first form)
~(if (not (seq? third))
third
`(my-recursive-infix ~third)))))
然而,使用解构来提前提取表单而不是就地更好一点:
(defmacro my-recursive-infix [form]
(let [[x op y] form]
`(~op ~x ~(if (not (seq? y))
y
`(my-recursive-infix ~y)))))
更好的是,真的是将非递归情况移到外面,这样(a)它适用于文字数字,(b)代码看起来更像它扩展到:
(defmacro my-recursive-infix [form]
(if-not (seq? form)
form
(let [[x op y] form]
`(~op ~x (my-recursive-infix ~y)))))