I am quite new to Lisp and was wondering: Is there a way to list all (user-defined) global variables?
One possibility would be to check which of the symbols of a package are boundp
:
(defun user-defined-variables (&optional (package :cl-user))
(loop with package = (find-package package)
for symbol being the symbols of package
when (and (eq (symbol-package symbol) package)
(boundp symbol))
collect symbol))
It returns a list of bound symbols that are not inherited from another package.
CL-USER> (user-defined-variables) ; fresh session
NIL
CL-USER> 'foo ; intern a symbol
FOO
CL-USER> (defun bar () 'bar) ; define a function
BAR
CL-USER> (defparameter *baz* 'baz) ; define a global variable
*BAZ*
CL-USER> (user-defined-variables)
(*BAZ*) ; only returns the global variable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With