Reputation: 131
I am quite new to Lisp and was wondering: Is there a way to list all (user-defined) global variables?
Upvotes: 4
Views: 776
Reputation: 14291
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
Upvotes: 7