Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Display all global variables.........

5 views
Skip to first unread message

Venkat

unread,
Apr 5, 2004, 9:08:18 AM4/5/04
to
Hi,

Is there any way to display all the global variables and their values.
Like if I loose track of how many variables I have what should I do?

Thanks
tvr1729

Matthieu Villeneuve

unread,
Apr 5, 2004, 9:39:52 AM4/5/04
to
"Venkat" <tvr...@yahoo.com> wrote in message
news:6f4d9f70.04040...@posting.google.com...

> Is there any way to display all the global variables and their values.

You may want to use DO-SYMBOLS:

(do-symbols (s)
(when (boundp s)
(format t "~A => ~A~%" s (symbol-value s))))


--
Matthieu Villeneuve


Luke Gorrie

unread,
Apr 5, 2004, 3:19:02 PM4/5/04
to
"Matthieu Villeneuve" <matthieuDOTvilleneuve@freeDOTfr> writes:

You might want to exclude imported symbols:

(defun my-variables (&optional (package *package*))
"Print the values of all bound variables in a package (the current one by default.)"
(do-symbols (sym package)
(when (and (boundp sym)
(eq (symbol-package sym) package))
(format t "~&~S = ~S~%" sym (symbol-value sym)))))

(my-variables)

Pascal Costanza

unread,
Apr 5, 2004, 4:17:41 PM4/5/04
to

Luke Gorrie wrote
:

> You might want to exclude imported symbols:
>
> (defun my-variables (&optional (package *package*))
> "Print the values of all bound variables in a package (the current one by default.)"
> (do-symbols (sym package)
> (when (and (boundp sym)
> (eq (symbol-package sym) package))
> (format t "~&~S = ~S~%" sym (symbol-value sym)))))
>
> (my-variables)

You mean:

(loop for sym being the present-symbols of *package*
when (boundp sym)
do (format t "~&~S = ~S~%" sym (symbol-value sym)))

;)


Pascal

--
1st European Lisp and Scheme Workshop
June 13 - Oslo, Norway - co-located with ECOOP 2004
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/

Thomas F. Burdick

unread,
Apr 7, 2004, 3:55:06 PM4/7/04
to
Pascal Costanza <cost...@web.de> writes:

> Luke Gorrie wrote
> :
> > You might want to exclude imported symbols:
> >
> > (defun my-variables (&optional (package *package*))
> > "Print the values of all bound variables in a package (the current one by default.)"
> > (do-symbols (sym package)
> > (when (and (boundp sym)
> > (eq (symbol-package sym) package))
> > (format t "~&~S = ~S~%" sym (symbol-value sym)))))
> >
> > (my-variables)
>
> You mean:
>
> (loop for sym being the present-symbols of *package*
> when (boundp sym)
> do (format t "~&~S = ~S~%" sym (symbol-value sym)))
>
> ;)

For the sake of completeness, these only note your bound variables,
not any globally special variables you might have that are unbound.

(defun specialp (symbol)
(eval
`(let ((thunk (let ((,symbol nil))
(lambda () ,symbol))))
(let ((,symbol t))
(funcall thunk)))))

(loop for sym being the present-symbols of *package*

if (boundp sym)


do (format t "~&~S = ~S~%" sym (symbol-value sym))

else if (specialp sym)
do (format t "~&~S is an unbound special variable.~%" sym))

--
/|_ .-----------------------.
,' .\ / | No to Imperialist war |
,--' _,' | Wage class war! |
/ / `-----------------------'
( -. |
| ) |
(`-. '--.)
`. )----'

Andrew Philpot

unread,
Apr 7, 2004, 4:58:12 PM4/7/04
to
In article <xcvisgb...@famine.OCF.Berkeley.EDU>, Thomas F. Burdick wrote:

> For the sake of completeness, these only note your bound variables,
> not any globally special variables you might have that are unbound.
>
> (defun specialp (symbol)
> (eval
> `(let ((thunk (let ((,symbol nil))
> (lambda () ,symbol))))
> (let ((,symbol t))
> (funcall thunk)))))
>
> (loop for sym being the present-symbols of *package*
> if (boundp sym)
> do (format t "~&~S = ~S~%" sym (symbol-value sym))
> else if (specialp sym)
> do (format t "~&~S is an unbound special variable.~%" sym))
>

I like this trick but I did find an edge case:

CL-USER(38): (specialp '*macroexpand-hook*)
Error: Funcall of NIL which is a non-function.
[condition type: SIMPLE-ERROR]

Restart actions (select using :continue):
0: Return to Top Level (an "abort" restart).
1: Abort entirely from this process.
[1] CL-USER(39):

Andrew

Kalle Olavi Niemitalo

unread,
Apr 8, 2004, 3:07:39 AM4/8/04
to
Andrew Philpot <phi...@isi.edu> writes:

> I like this trick but I did find an edge case:
>
> CL-USER(38): (specialp '*macroexpand-hook*)
> Error: Funcall of NIL which is a non-function.
> [condition type: SIMPLE-ERROR]

Consider also (specialp 'nil), which you could handle with
CONSTANTP; or (specialp '*read-base*), where an implementation
could conceivably enforce that the value is indeed a radix.

Should I ever need to check whether a symbol has been proclaimed
as a special variable (independently of whether it is BOUNDP),
I think I'd prefer implementation-specific code over this trick.
It is in general not OK to bind a special variable if you don't
know how it's used.

Kalle Olavi Niemitalo

unread,
Apr 8, 2004, 3:57:13 AM4/8/04
to
Kalle Olavi Niemitalo <k...@iki.fi> writes:

> Consider also (specialp 'nil), which you could handle with
> CONSTANTP; or (specialp '*read-base*), where an implementation
> could conceivably enforce that the value is indeed a radix.

On third thought, (specialp '*compile-file-pathname*) may be
a better example. "The consequences are unspecified if an
attempt is made to assign or bind either of these variables."

Thomas F. Burdick

unread,
Apr 8, 2004, 4:26:25 PM4/8/04
to
Andrew Philpot <phi...@isi.edu> writes:

> In article <xcvisgb...@famine.OCF.Berkeley.EDU>, Thomas F. Burdick wrote:
>
> > For the sake of completeness, these only note your bound variables,
> > not any globally special variables you might have that are unbound.
> >
> > (defun specialp (symbol)
> > (eval
> > `(let ((thunk (let ((,symbol nil))
> > (lambda () ,symbol))))
> > (let ((,symbol t))
> > (funcall thunk)))))
> >
> > (loop for sym being the present-symbols of *package*
> > if (boundp sym)
> > do (format t "~&~S = ~S~%" sym (symbol-value sym))
> > else if (specialp sym)
> > do (format t "~&~S is an unbound special variable.~%" sym))
> >
>
> I like this trick but I did find an edge case:
>
> CL-USER(38): (specialp '*macroexpand-hook*)
> Error: Funcall of NIL which is a non-function.
> [condition type: SIMPLE-ERROR]

Yeah, it's obviously not the kind of thing you can do on the
implementation itself. Outside of implementation packages, it should
be fine (except on SBCL); it's definately a dirty trick, though.

0 new messages