The less-familiar parts of Lisp for beginners — symbol-package

The symbol-package function can be used to locate the original package in which a symbol is defined.  Because it is possible to import symbols into other packages, it may sometimes not be clear what the origin is of a symbol being used.  Here is a transcript that demonstrates the use of symbol-package.  In it, we create a package, and a function in that package.  We then create a second package and import the symbol corresponding to the function.  Outside of either package, we can invoke the function in the second package, but symbol-package reveals that it is actually found in the first.
*slime-repl sbcl*

CL-USER> (defpackage :base (:use :common-lisp))
#<PACKAGE "BASE">
CL-USER> (in-package :base)
#<PACKAGE "BASE">
BASE> (defun myfunc () (format t "hi~%"))
MYFUNC
BASE> (defpackage :derived (:use :common-lisp))
#<PACKAGE "DERIVED">
BASE> (in-package :derived)
#<PACKAGE "DERIVED">
DERIVED> (import 'base::myfunc)
T
DERIVED> (myfunc)
hi
NIL
DERIVED> (in-package :cl-user)
#<PACKAGE "COMMON-LISP-USER">
CL-USER> (derived::myfunc)
hi
NIL
CL-USER> (symbol-package 'derived::myfunc)
#<PACKAGE "BASE">

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.