Get list of user created variables

2019-06-28 01:13发布

问题:

I want to get a list of all variables that I have created in a lisp session. I think that this should be possible by looking at all symbols interned in common-lisp-user. But how can I get such a list?

回答1:

To get bound variables only from cl-user you iterate all bound symbols with do-symbols and exclude the symbols, that are imported from other packages:

(let ((external-symbols (mapcan (lambda (pkg)
                                  (let (rez)
                                    (do-symbols (s pkg rez)
                                      (push s rez))))
                                (package-use-list (find-package 'cl-user)))))
  (do-symbols (s 'cl-user)
    (when (and (boundp s)
               (not (member s external-symbols)))
      (print s))))


回答2:

You can use do-symbols to find the symbols in the common-lisp-user package.

See the CLHS entry for Macro DO-SYMBOLS, DO-EXTERNAL-SYMBOLS, DO-ALL-SYMBOLS