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
"Venkat" <·······@yahoo.com> wrote in message
·································@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
"Matthieu Villeneuve" <·····················@freeDOTfr> writes:
> "Venkat" <·······@yahoo.com> wrote in message
> ·································@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))))
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)
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/
Pascal Costanza <········@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! |
/ / `-----------------------'
( -. |
| ) |
(`-. '--.)
`. )----'
In article <···············@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
Andrew Philpot <·······@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 <···@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."
Andrew Philpot <·······@isi.edu> writes:
> In article <···············@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.
--
/|_ .-----------------------.
,' .\ / | No to Imperialist war |
,--' _,' | Wage class war! |
/ / `-----------------------'
( -. |
| ) |
(`-. '--.)
`. )----'