问题
I am currently trying to understand how Common Lisp manages its packages and modules.
Consider this code:
(in-package :cl-user)
(ql:quickload :cl-who)
(ql:quickload :hunchentoot)
(ql:quickload :parenscript)
(defpackage :retro-games
(:use :cl :cl-who :hunchentoot :parenscript))
(in-package :retro-games)
(defclass game ()
((name :initarg :name)
(votes :initform 0)))
When I create a Slime buffer and then run this code in another buffer that is set to Slime mode, I see some prints in the first buffer that is *slime-repl sbcl*
:
Load 1 ASDF system: cl-who ; Loading "cl-who"
To load "parenscript": Load 1 ASDF system: parenscript ; Loading "parenscript" ..
To load "hunchentoot": Load 1 ASDF system: hunchentoot ; Loading "hunchentoot" ..............
That is, there seems to be some "talking" going on between the two buffers. However, if I try to run (make-instance game("Chess"))
, I get an error because the CL-USER
package does not know about the game
class.
On the other hand if I run (in-package :retro-games)
in the slime repl sbcl
buffer, I am able to run (make-instance game("Chess"))
.
The question is how does Common Lisp organize its work with packages? On related note, what is the difference between a buffer being a slime repl sbcl
and a buffer being in slime-mode
?
Are the packages at all similar to Python's virtual environments? That is, where are the CL-WHO
, HUNCHENTOOT
and PARENSCRIPT
being installed in my case? Do I have any choice over this?
回答1:
First, try (make-instance 'game :name "Chess")
.
Note that Common Lisp uses prefixed notation between paarenthesis. So, if want to call a function like foo("bar")
, it would be (foo "bar")
.
If you want, you can create a function like:
(defun new-game (game-name)
(make-instance 'game :name game-name))
If you are using Quicklisp, the packages me be installed at where you've installed quicklisp. If it is installed at your home folder, the packages should be at /home/user/quicklisp/dists/quicklisp/software/
.
来源:https://stackoverflow.com/questions/46609034/common-lisp-package-and-module-management