From: Jordan Rosenthal
Subject: Help with macro
Date: 
Message-ID: <3C14DD3C.F56D3A9F@ll.mit.edu>
Hi,

I'm trying to create a macro called dbg-msg that can be used like

  (dbg-msg x y z)

where x, y, and z are arbitrary symbols.  This should print out
something like

x = 5
y = (1 2 3)
z = unbound

depending on the variables values (or lack-thereof).  Note that when a
symbol is unbound, it should print out a corresponding message and not
generate an error.

I have a macro which works correctly at the toplevel.  To find out if a
variable has been bound it uses the boundp function.  I can't use this
function, however, for lexically defined variables because the boundp
function doesn't work in this case.

Is there a technique to allow me to do what I want.  Note: I'm pretty
new to lisp.

Thanks,

Jordan

From: Barry Margolin
Subject: Re: Help with macro
Date: 
Message-ID: <hs5R7.2$EZ1.187220@burlma1-snr2>
In article <·················@ll.mit.edu>,
Jordan Rosenthal  <··@ll.mit.edu> wrote:
>Hi,
>
>I'm trying to create a macro called dbg-msg that can be used like
>
>  (dbg-msg x y z)
>
>where x, y, and z are arbitrary symbols.  This should print out
>something like
>
>x = 5
>y = (1 2 3)
>z = unbound
>
>depending on the variables values (or lack-thereof).  Note that when a
>symbol is unbound, it should print out a corresponding message and not
>generate an error.
>
>I have a macro which works correctly at the toplevel.  To find out if a
>variable has been bound it uses the boundp function.  I can't use this
>function, however, for lexically defined variables because the boundp
>function doesn't work in this case.
>
>Is there a technique to allow me to do what I want.  Note: I'm pretty
>new to lisp.

Instead of checking if the variable is bound before printing the value, use
a condition handler to catch the UNBOUND-VARIABLE error that occurs if you
try to access an unbound variable:

(defmacro dbg-msg (&rest variables)
  `(progn ,@(loop for var in variables
                  collect
                  `(handler-case
                       (format *debug-output* "~&~S = ~S~%" ',var ,var)
                     (unbound-variable ()
                       (format *debug-output* "~&~S = unbound~%" ',var))))))

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Jordan Rosenthal
Subject: Re: Help with macro
Date: 
Message-ID: <3C1604D9.BF5DFFE7@ll.mit.edu>
Thanks!

Jordan


Barry Margolin wrote:

> In article <·················@ll.mit.edu>,
> Jordan Rosenthal  <··@ll.mit.edu> wrote:
> >Hi,
> >
> >I'm trying to create a macro called dbg-msg that can be used like
> >
> >  (dbg-msg x y z)
> >
> >where x, y, and z are arbitrary symbols.  This should print out
> >something like
> >
> >x = 5
> >y = (1 2 3)
> >z = unbound
> >
> >depending on the variables values (or lack-thereof).  Note that when a
> >symbol is unbound, it should print out a corresponding message and not
> >generate an error.
> >
> >I have a macro which works correctly at the toplevel.  To find out if a
> >variable has been bound it uses the boundp function.  I can't use this
> >function, however, for lexically defined variables because the boundp
> >function doesn't work in this case.
> >
> >Is there a technique to allow me to do what I want.  Note: I'm pretty
> >new to lisp.
>
> Instead of checking if the variable is bound before printing the value, use
> a condition handler to catch the UNBOUND-VARIABLE error that occurs if you
> try to access an unbound variable:
>
> (defmacro dbg-msg (&rest variables)
>   `(progn ,@(loop for var in variables
>                   collect
>                   `(handler-case
>                        (format *debug-output* "~&~S = ~S~%" ',var ,var)
>                      (unbound-variable ()
>                        (format *debug-output* "~&~S = unbound~%" ',var))))))
>
> --
> Barry Margolin, ······@genuity.net
> Genuity, Woburn, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: JP Massar
Subject: Re: Help with macro
Date: 
Message-ID: <3c14e6a2.208932311@netnews.attbi.com>
On Mon, 10 Dec 2001 11:05:16 -0500, Jordan Rosenthal <··@ll.mit.edu>
wrote:

>Hi,
>
>I'm trying to create a macro called dbg-msg that can be used like
>
>  (dbg-msg x y z)
>
>where x, y, and z are arbitrary symbols.  This should print out
>something like
>
>x = 5
>y = (1 2 3)
>z = unbound
>
>depending on the variables values (or lack-thereof).  Note that when a
>symbol is unbound, it should print out a corresponding message and not
>generate an error.
>
>I have a macro which works correctly at the toplevel.  To find out if a
>variable has been bound it uses the boundp function.  I can't use this
>function, however, for lexically defined variables because the boundp
>function doesn't work in this case.
>
>Is there a technique to allow me to do what I want.  Note: I'm pretty
>new to lisp.
>
 
How are you proposing to distinguish between non-lexically scoped
variables and lexically scoped variables within the context of your
macro?

Is it in fact possible to create a lexically scoped variable which is
unbound?  (LET always binds to NIL if no value is provided).

Rather than using BOUNDP, you can use HANDLER-CASE to catch the
unbound error condition:

(HANDLER-CASE ghoodao
  (unbound-variable () "ghoodao is unbound"))

This will work with lexically and non-lexically scoped variables.