Question

This is my first time working with packages in lisp. I have the general gist of how they work in terms of encapsulating data and code. It seems similar enough to other languages that I don't need to worry about the differences just yet.

However, I am running into a problem where if I set something in my code as:

(setf baseNames '(coffee wax blue chili stink green soy blackeyed garden))

It will then come out later as:

(print baseNames)
(PIZZABAGEL::COFFEE PIZZABAGEL::WAX PIZZABAGEL::BLUE PIZZABAGEL::CHILI PIZZABAGEL::STINK PIZZABAGEL::GREEN

PIZZABAGEL::SOY PIZZABAGEL::BLACKEYED PIZZABAGEL::GARDEN)

Pizzabagel is the name of my package. Is this normal behavior? Let's say I want to do something like:

(position 'WAX baseNames)

I would expect that this would return 1, but since the package name is prepending itself everywhere, it returns nil. I want to know how I can a) make it not prepend everywhere or b) what's the normal practice for working with this? I am currently writing my own position function that I hope handles this case, but it seems like the wrong approach to have to do that in this situation. Thanks in advance for any tips or pointers you might be able to provide.

Was it helpful?

Solution

What you're running in to is some package confusion.

Whenever you read a symbol with the reader, the symbol implicitly inherits the package name of the current package.

You can see this demonstrated here:

1]> (setf baseNames '(coffee wax blue chili stink green soy blackeyed garden))
(COFFEE WAX BLUE CHILI STINK GREEN SOY BLACKEYED GARDEN)
[2]> (export 'basenames)
T
[3]> (defpackage so (:use cl-user cl))
#<PACKAGE SO>
[4]> (in-package so)
#<PACKAGE SO>
SO[5]> (print basenames)

(COMMON-LISP-USER::COFFEE COMMON-LISP-USER::WAX COMMON-LISP-USER::BLUE
 COMMON-LISP-USER::CHILI COMMON-LISP-USER::STINK COMMON-LISP-USER::GREEN
 COMMON-LISP-USER::SOY COMMON-LISP-USER::BLACKEYED COMMON-LISP-USER::GARDEN) 
(COMMON-LISP-USER::COFFEE COMMON-LISP-USER::WAX COMMON-LISP-USER::BLUE
 COMMON-LISP-USER::CHILI COMMON-LISP-USER::STINK COMMON-LISP-USER::GREEN
 COMMON-LISP-USER::SOY COMMON-LISP-USER::BLACKEYED COMMON-LISP-USER::GARDEN)

By default, you typically start off in the CL-USER package, which is the case here. CLISP doesn't prompt the package name when it's CL-USER (you can see it adding SO to the prompt later when we switch to the SO package).

So, anyway, what's happening is that you have you packages confused.

Consider:

SO[6]> (position 'wax basenames)
NIL

This fails because it's looking for SO::WAX in basenames (because we're in the SO package), not COMMON-LISP-USER::WAX. Which is what is in the list. That's why this fails. (CL-USER is an abbreviation/alias for COMMON-LISP-USER)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top