I am trying to write what I thought should be a simple function to
display the name and value of a variable, but I am having trouble and
need some advice from the experts (I am relatively new at Lisp).
I currently use the following statement to display the name and value
of a symbol (say CmdLst), mainly to help me debug:
(format t "CmdLst: ~A~%" CmdLst)
which works fine, but since I have to enter such commands often, I
wanted to save myself some typing and use a function instead, so that
I could just enter
(show CmdLst)
My first try was the following:
(defun show (var)
(format t "~A: ~A~%" (symbol-name var) (eval var)))
There are three problems with this function:
1. I have to single-quote the argument:
(show 'CmdLst)
This is a small enough problem that I could live with it.
2. In the output, the name is all capitalized:
CMDLST: (1 2 3)
3. The worst problem is that, except when CmdLst is a global
variable, when it is sent to the "show" function (as a quoted symbol)
it goes outside of its scope and hence can't be evaluated.
My second try was to send in the symbol as a string:
(defun show (VarStr)
(format t "~A: ~A~%" VarStr (eval (read-from-string VarStr))))
so that it would be called thusly: (show "CmdLst")
This solves the second problem but not the first or third.
I would like to avoid having to call the function by passing in two
values - e.g., (show "CmdLst" CmdLst) - since that is hardly shorter
than not using a function at all.
Is there any way I can write such a function so that I can call it
with only one argument? Ideally I'd like to be able to call it like
this:
(show CmdLst)
but I'd be happy with
(show 'CmdLst)
or
(show "CmdLst").
From: Paul F. Dietz
Subject: Re: Displaying symbol name and value
Date:
Message-ID: <3D0B4CC7.48CDD815@dls.net>
Steven Sommer wrote:
> Is there any way I can write such a function so that I can call it
> with only one argument?
Use a macro.
(demacro show (var)
`(format t "~A: ~A~%" ',var ,var))
Paul
On Sat, 15 Jun 2002 14:18:15 GMT, "Paul F. Dietz" <·····@dls.net>
wrote:
>Steven Sommer wrote:
>
>> Is there any way I can write such a function so that I can call it
>> with only one argument?
>
>Use a macro.
>
>(demacro show (var)
> `(format t "~A: ~A~%" ',var ,var))
>
> Paul
Thanks very much for your help! That is exactly what I needed. It
looks like I should make "Learn how to write macros" #1 on my Lisp
to-do list.
From: Paul F. Dietz
Subject: Re: Displaying symbol name and value
Date:
Message-ID: <3D0B8BD7.A21A6142@dls.net>
Steven Sommer wrote:
> Thanks very much for your help! That is exactly what I needed. It
> looks like I should make "Learn how to write macros" #1 on my Lisp
> to-do list.
Good idea -- macros are arguably the heart of Lisp.
Try Graham's 'On Lisp' for many examples. It's available
for free download: http://www.paulgraham.com/onlisptext.html
Paul
"Paul F. Dietz" <·····@dls.net> wrote in message
······················@dls.net...
> Steven Sommer wrote:
>
> > Is there any way I can write such a function so that I can
> > call it with only one argument?
>
> Use a macro.
>
> (demacro show (var)
> `(format t "~A: ~A~%" ',var ,var))
>
> Paul
I'm using LWW 4.2.6. When I use the SHOW macro above together
with a compiler macro that issues a warning for any uses of SHOW,
I get what seems to me to be strange behaviour; the transcript
below shows what happens.
Is this behaviour to be expected or should I file a bug report
with Xanalys?
_________________
Define SHOW:
CL-USER 1 > (defmacro show (var)
`(format t "~A: ~A~%" ',var ,var))
SHOW
Define a function FOO that calls SHOW:
CL-USER 2 > (defparameter *fred* 789)
*FRED*
CL-USER 3 > (defun foo () (let* ((x *fred*)) (show x)))
FOO
FOO works as I expect, both interpreted and compiled:
CL-USER 4 > (foo)
X: 789
NIL
CL-USER 5 > (compile 'foo)
FOO
NIL
NIL
CL-USER 6 > (foo)
X: 789
NIL
Define a compiler macro that warns when SHOW is used:
CL-USER 7 > (define-compiler-macro show (&whole form x)
(declare (ignore x))
(warn "SHOW used")
form)
SHOW
Define FOO again so that we revert to an interpreted version:
CL-USER 8 > (defun foo () (let* ((x *fred*)) (show x)))
FOO
Still ok:
CL-USER 9 > (foo)
X: 789
NIL
Get a warning when we compile FOO, as expected:
CL-USER 10 > (compile 'foo)
;;;*** Warning in FOO: SHOW used
FOO
((FOO #<SIMPLE-WARNING 20603984>))
T
But now the output from FOO is different and not what I expect:
CL-USER 11 > (foo)
#<COMPILER::VAR-REF #<Venv 543176964 X>>: 789
NIL