I am creating a macro that will accept the sum in infix notation and return the equivalent list in prefix notation. Although the code is probably not yet logically correct, when I called the macro, I ran into the following error:
CompilerException java.lang.IllegalArgumentException: Mismatched argument count to recur, expected: 0 args, got: 5, compiling:(/tmp/form-init1201331851685991945.clj:1:1)
Here is the macro code:
(defmacro infixer [o1 outlist i1 i2 i3] `(if (empty? ~i3) (concat ~i1 ~i2) (if (= ~i2 ~o1) ;then recur list of i2 i1 i3 at outlist tail (recur ~o1 (concat ~outlist (~i2 ~i1 ~(first i3))) () ~(second i3) ~(rest (rest i3))) ;else recur i1 at outlist tail (recur ~o1 (concat ~outlist ~i1) ~i2 ~i3 ~(rest i3) ))))
I use the following to check it, after which the above error is raised:
(def s1 '(1 + 3 * 2 / 2 * 5 - 3)) (infixer * () (first s1)(second s1)(rest (rest s1)))
I don’t see any recur targets, so shouldn’t recur see five arguments after the macro name? I have not used syntax quoting before, so my suspicion is that I misunderstood how to use it. Can someone explain why the error occurs?
Also, if I give up the logic of trying to convert an infix to a prefix, feel free to point me in the right direction! I am new to Clojure and FP, so any guidance is extremely appreciated - thanks in advance.
source share