Hi, I am learning Clojure macros and I am trying to write a macro that translates infix forms into prefix forms as follows: (9 + (1 * 3)) => (+ 9 (* 1 3))
(defn infix [form]
(list (second form) (first form) (nth form 2)))
(defmacro r-infix [form]
(if (coll? form)
(map r-infix (infix form))
form))
(r-infix (9 + (1 * 2)));;=>ArityException
But if you define a macro as follows, it works fine:
(defn infix [form]
(list (second form) (first form) (nth form 2)))
(defn r-infix-fn [form]
(if (coll? form)
(map r-infix-fn (infix form))
form))
(defmacro r-infix [form]
(r-infix-fn form))
(r-infix (9 + (1 * 2)));;=>11
It’s hard for me to debug the first example, can someone help me?
source
share