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))
CL-USER> (in-package :base)
BASE> (defun myfunc () (format t "hi~%"))
BASE> (defpackage :derived (:use :common-lisp))
BASE> (in-package :derived)
DERIVED> (import 'base::myfunc)
DERIVED> (myfunc)
DERIVED> (in-package :cl-user)
CL-USER> (derived::myfunc)
CL-USER> (symbol-package 'derived::myfunc)

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.