Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing currently bound global variables in lisp

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

like image 775
ben_za Avatar asked Mar 17 '23 23:03

ben_za


1 Answers

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
like image 156
danlei Avatar answered Mar 27 '23 11:03

danlei