I'm trying to import some symbols from a package I've created, but I
can't seem to get it to work. I'm using Corman Lisp.
I've created a file called string.lisp, containing the code I want to
create:
(defpackage #:string
(:export
#:strcat
))
(in-package #:string)
(defun strcat (&rest strings)
"Concatenate strings together"
(flet ((join (a b) (concatenate 'string a b)))
(reduce #'join strings)))
(provide 'string)
;;; --- end of file string.lisp
I then create a file called string-example.lisp, which I want to use to
call strcat (without "qualifying" the function when I call it):
(require 'string)
(import 'string::strcat)
(strcat "mark" " carter" " was here")
;;; --- end of file string-example.lisp
However, at the call to strcat, it says
the function STRCAT is undefined
I know I must be doing something stupid wrong, but I can't seem to make
it work.
Mark Carter wrote:
> I'm trying to import some symbols from a package I've created, but I
> can't seem to get it to work. I'm using Corman Lisp.
>
>
> I've created a file called string.lisp, containing the code I want to
> create:
>
> (defpackage #:string
> (:export
> #:strcat
>
> ))
>
> (in-package #:string)
>
>
> (defun strcat (&rest strings)
> "Concatenate strings together"
> (flet ((join (a b) (concatenate 'string a b)))
> (reduce #'join strings)))
>
> (provide 'string)
> ;;; --- end of file string.lisp
>
> I then create a file called string-example.lisp, which I want to use to
> call strcat (without "qualifying" the function when I call it):
>
> (require 'string)
> (import 'string::strcat)
>
> (strcat "mark" " carter" " was here")
>
> ;;; --- end of file string-example.lisp
>
> However, at the call to strcat, it says
> the function STRCAT is undefined
Hm, maybe you are compiling the code, so the compiler doesn't see
'strcat because import is only executed at runtime. (But I am just
guessing here.)
Anyway, the real issue here is that you use 'import which is a low-level
function that works on a per-symbol basis. There are two issues here: a)
It's much more convenient to get access to all exported symbols of a
package at once (that's why the notion of an exported symbol makes sense
in the first place), and b) almost all important package-related
functionality can be better used in a more declarative way using the
'defpackage macro.
The best solution here, IMHO, is to do the following:
(defpackage :string-example
(:use :string :common-lisp))
...and then use that package in your string-example file:
(in-package :string-example)
It's also possible to say (use-package :string), but this is again only
executed at runtime.
Pascal
--
3rd European Lisp Workshop
July 3-4 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
Pascal Costanza wrote:
> The best solution here, IMHO, is to do the following:
>
> (defpackage :string-example
> (:use :string :common-lisp))
>
> ...and then use that package in your string-example file:
>
> (in-package :string-example)
Ta. Also, I've jiggled the way I've declared things to conform to the
way that Peter Siebel does it in his chapter on packages.
Must admit, I was getting a bit frustrated trying to get it to work.
Pascal Costanza <··@p-cos.net> wrote:
+---------------
| Mark Carter wrote:
| > (require 'string)
| > (import 'string::strcat)
| >
| > (strcat "mark" " carter" " was here")
...
| > However, at the call to strcat, it says
| > the function STRCAT is undefined
|
| Hm, maybe you are compiling the code, so the compiler doesn't see
| 'strcat because import is only executed at runtime. (But I am just
| guessing here.)
+---------------
Sounds like a good guess to me. If he's compiling the using program,
the symbol STRCAT will get INTERN'd in the using code's package
*before* the REQUIRE & IMPORT get done. I'll bet the following
would work better:
(eval-when (:compile-toplevel :load-toplevel :execute)
(require 'string)
(import 'string::strcat))
(strcat "mark" " carter" " was here")
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607