Listing currently related global variables in lisp

I am new to Lisp and wondered: Is there a way to list all (user-defined) global variables?

+4
source share
1 answer

One option is to check which of the package characters 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 related characters 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
+7
source

All Articles