a macro to debug the let form

55 views
Skip to first unread message

Sunil S Nandihalli

unread,
Nov 25, 2010, 2:57:45 AM11/25/10
to clo...@googlegroups.com
Hello everybody,
 I was trying to learn to write clojure macros and the code is posted here in the following link
https://gist.github.com/715047
There are basically three macros

1. with-seperator - a zero argument macro and is supposed to just draw a line to indicate beginning and ending of the execution of the body.

2. display-local-bindings - a function to print the local bindings in the lexical scope where the macro is called

3. letd - a helper macro to print the values of all the bindings  followed by printing of the local bindings using display-local-bindings

The letd macro as posted works as expected but without the seperation line .  It is supposed to print the seperation line when I uncomment line 14 and comment line 15 but some how this is causing the &env variable automatically passed with every macro to be nil display-local-binding .. but the I feel it is not the case .. can somebody help me understand this. This was an exercise to learn macro writing than to writing a letd debugging helper function..

Thanks,
Sunil.

Sunil S Nandihalli

unread,
Nov 25, 2010, 9:13:48 AM11/25/10
to clo...@googlegroups.com
I just tried to re-write with-seperator without using the symbol-macros from macro-utils and it seems to work fine .. 

Robert McIntyre

unread,
Nov 27, 2010, 12:36:38 PM11/27/10
to clo...@googlegroups.com
cool! Although I think with-seperator should be spelled "with-separator"

--Robert McIntyre

On Thu, Nov 25, 2010 at 9:13 AM, Sunil S Nandihalli

> --
> 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

Sunil S Nandihalli

unread,
Nov 28, 2010, 3:32:37 AM11/28/10
to clo...@googlegroups.com
Hi Robert,
 What I had posted before was half-baked buggy code .. the following gist has a few more helper debug macros 
https://gist.github.com/718725

This code was written when I was trying to learn writing macros .. so any criticism is very welcome.
Thanks
Sunil.

Alan

unread,
Dec 7, 2010, 1:25:53 PM12/7/10
to Clojure
I see you have defined a print-and-return macro; you might prefer my
and-print:

(defmacro and-print
"A useful debugging tool when you can't figure out what's going on:
wrap a form with and-print, and the form will be printed alongside
its result. The result will still be passed along."
[val]
`(let [x# ~val]
(println '~val "is" x#)
x#))

I'm not sure what you're using the flag argument for; if it's to keep
track of what prints belong to what statements then my automatic
printing of the input form might be useful for you.

I'd pull the (repeat blah blah blah) stuff in with-separator out into
an optional argument, so that (a) you only have to write it once, and
(b) you can use a different separator easily.

I find the map in local-bindings pretty hard to read - what on earth
is `'~x# for? Without trying to understand what you're doing, it seems
like you could gain readability by replacing (list 'println [`'~x#
x#]) with `(println ['~x# x#])

On Nov 28, 12:32 am, Sunil S Nandihalli <sunil.nandiha...@gmail.com>
wrote:
> > > clojure+u...@googlegroups.com<clojure%2Bunsu...@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 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<clojure%2Bunsu...@googlegroups.com>

Sunil S Nandihalli

unread,
Dec 9, 2010, 3:08:29 AM12/9/10
to clo...@googlegroups.com
Thanks Alan,
 I have in fact realized the mistake after I posted it .. and I had posted another message which has it the way you mentioned. 
Sunil.
Reply all
Reply to author
Forward
0 new messages