Dear Clojure Group,
I wanted to expand the 'infix' macro presented in chapter 7.3.1 of
'Clojure in Action' to handle nested s-expressions:
My first version did not work:
(defmacro my-infix [expr]
(if (coll? expr)
(let [ [left op right] expr]
(list op (my-infix left) (my-infix right)))
expr))
;; Test:
(my-infix ((3 * 5) - (1 + 1)))
;; Wrong number of args (1) passed to: user$my-infix
;; [Thrown class java.lang.IllegalArgumentException]
However, the following version does work:
(defmacro new-infix [expr]
(if (coll? expr)
(let [ [left op right] expr]
`(~op (new-infix ~left) (new-infix ~right)))
expr))
(new-infix ((3 * 5) - (1 + 1)))
;; --> 13
(macroexpand '(new-infix ((3 * 5) - (1 + 1))))
;; (- (user/new-infix (3 * 5)) (user/new-infix (1 + 1)))
I would love to know why the first version 'my-infix' throws an
exception. Can anybody give me a hint? I hope the answer could give me
a better understanding of how macros work.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en