Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to `defun` a function in a package?

I'm sorry, this is a very newbie Common Lisp question.

I'm learning common-lisp and package system.

I started with "The Complete Idiot's Guide to Common Lisp Packages" from http://cl-cookbook.sourceforge.net/packages.html

In Chapter 1, the author defined a function foo in the package :bob

? (make-package :bob)
#<Package "BOB">
? (make-package :jane)
#<Package "JANE">
? (in-package bob)
#<Package "BOB">
? (defun foo () "This is Bob's foo")
FOO

I tested this code line by line in my REPL, but failed:

; SLIME 2.26
CL-USER> (make-package :bob)
#<PACKAGE "BOB">
CL-USER> (make-package :jane)
#<PACKAGE "JANE">
CL-USER> (in-package bob)
#<COMMON-LISP:PACKAGE "BOB">
BOB> (defun foo () "This is Bob's foo")
; in: DEFUN FOO
;     (BOB::DEFUN BOB::FOO NIL "This is Bob's foo")
; 
; caught COMMON-LISP:STYLE-WARNING:
;   undefined function: BOB::DEFUN
; 
; caught COMMON-LISP:WARNING:
;   undefined variable: BOB::FOO
; 
; compilation unit finished
;   Undefined function:
;     DEFUN
;   Undefined variable:
;     FOO
;   caught 1 WARNING condition
;   caught 1 STYLE-WARNING condition

And it told me:

The variable FOO is unbound.
   [Condition of type COMMON-LISP:UNBOUND-VARIABLE]

What's the problem?

How to fix this problem and make it work?

Very thanks.

PS: My environment is SBCL + quicklisp + slime.

like image 323
chansey Avatar asked Oct 16 '25 16:10

chansey


1 Answers

You need to tell Lisp which package to use. In standard Common Lisp it is unspecified which packages to use. SBCL uses none. If you want symbols of the package CL to be available, you have to explicitly use that package.

Package JANE

* (make-package :jane)
#<PACKAGE "JANE">
* (describe *)
#<PACKAGE "JANE">
  [package]


0 internal symbols.

For package BOB we use package CL:

* (make-package "BOB" :use '("CL"))
#<PACKAGE "BOB">
* (describe *)
#<PACKAGE "BOB">
  [package]


Use-list: COMMON-LISP
0 internal symbols.


* (find-symbol "DEFUN" "BOB")
DEFUN
:INHERITED

The symbol DEFUN is available in package BOB.

* (find-symbol "DEFUN" "JANE")
NIL
NIL

The symbol DEFUN is NOT available in package JANE.

like image 90
Rainer Joswig Avatar answered Oct 19 '25 12:10

Rainer Joswig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!