Hi again,
just for the fun of doing it, here's a recursive version of infix:
--8<---------------cut here---------------start------------->8---
(defmacro infix [x]
(if (sequential? x)
`(~(second x) (infix ~(first x)) (infix ~(nth x 2)))
x))
--8<---------------cut here---------------end--------------->8---
That works fine no matter if you use vectors or lists, because I simply
pick the operator and the 2 args manually.
I think it's educational to follow a complete macroexpansion process.
Here's an example:
--8<---------------cut here---------------start------------->8---
(let [a 3, b 2, c 3]
(infix ((a * b) + [c * c])))
Expands to:
(let* [a 3 b 2 c 3]
(+ (infix (a * b)) (infix [c * c])))
Expands to:
(let* [a 3 b 2 c 3]
(+ (* (infix a) (infix b)) (infix [c * c])))
Expands to:
(let* [a 3 b 2 c 3]
(+ (* a (infix b)) (infix [c * c])))
Expands to:
(let* [a 3 b 2 c 3]
(+ (* a b) (infix [c * c])))
Expands to:
(let* [a 3 b 2 c 3]
(+ (* a b) (* (infix c) (infix c))))
Expands to:
(let* [a 3 b 2 c 3]
(+ (* a b) (* c (infix c))))
Finally, expands to:
(let* [a 3 b 2 c 3]
(+ (* a b) (* c c)))
--8<---------------cut here---------------end--------------->8---
HTH,
Tassilo
--
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