Clojure definition of a recursive macro

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?

+4
source share
1 answer

Macros in Clojure are not “first-class citizens,” meaning they cannot be used in all the ways that data and functions (which are not macros) can possess.

You cannot match a macro: - (

, , . , .

Me: hello , .

-Advanced-Compiler: , , , , , , ???

Me: , , -, .

-Advanced-Compiler: : -)

- , , , . , , , .

-, -, , - . , .

, ( ), . - r-inflix-fn - , , , , .

, .

+2

Source: https://habr.com/ru/post/1690569/


All Articles