mexpand on macrolet

109 views
Skip to first unread message

Ritchie Cai

unread,
Dec 5, 2015, 2:45:38 AM12/5/15
to Clojure
I'm not sure how to print a macroexpand on macros that defined in macrolet or symbol-macrolet.

with normal macros, I can do:

(defmacro test-macro [] '(+ 1 2))
(pprint (macroexpand-1 '(test-macro))) 

;; (+ 1 2)
;; nil

but with macrolet or symbol-macrolet:

(symbol-macrolet [(b [] '(+ 1 2))]
                               (pprint (mexpand-1 '(b))))

;; (b)
;; nil

(symbol-macrolet [(b [] '(+ 1 2))]
                               (pprint (mexpand-1 (b))))

;; 3
;; nil

what am I doing wrong?

Thanks
Ritchie

retnuH

unread,
Dec 7, 2015, 11:09:44 AM12/7/15
to Clojure
This worked for me (after some experimentation):

(pprint (mexpand-1 '(symbol-macrolet [b (+ 1 2)] b)))

;; (do (+ 1 2))
;; nil


Cheers, 

H

gianluca torta

unread,
Dec 7, 2015, 2:14:46 PM12/7/15
to Clojure
Hi Hunter,

however, in this way you are expanding the application of the macro "symbol-macrolet" (which is itself a macro), not just the symbol macro "b":

(pprint (mexpand-1 '(symbol-macrolet [b (+ 1 2)] b ["something" "else"])))
;; (do (+ 1 2) ["something" "else"])
;; nil

cheers,
Gianluca

Hunter Kelly

unread,
Dec 7, 2015, 3:06:52 PM12/7/15
to clo...@googlegroups.com
True!  But from what I understand of the way symbol-macrolet works, it's not possible to do one without the other.  symbol-macrolet actually looks for only symbols in the forms you pass it after the let bindings; it won't traverse inside any quoted forms.  It also takes care not to override the symbols that are shadowed by contained let statements, etc.

But I could be wrong - this was just after a quick look to see how it worked.

Cheers,

H

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/TbDrw-YuQQc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages