How is Lisp code structured? What are Packages and Systems?
I'm learning Lisp (SBCL) an开发者_Go百科d I'm confused about how lisp code is structured. I believe that Lisp Packages are like Python's Modules?
I want to do something like this.
utilities.py:
def foo():
print "And there is silence!"
main.py:
import utilities as u
u.foo()
I've looked up packages, loading and asdf systems. I still don't understand how it all fits together.
Regarding the comparison to Python:
Packages are the most similar thing in CL to Python modules since both are namespaces. Systems and system tools (like ASDF) are more similar to stuff like Distutils and Setuptools in Python.
The most important difference between Python modules and CL packages: packages are independent from files and directories. Instead you use defpackage
to create packages and use-package
to place the following code into a specific package. There are other package-related operators, but they are mostly for interactive use, these two are the most important in source files.
See the relevant chapter in Practical Common Lisp to get more detailed information.
A package is a namespace for symbols. One can import and export symbols from and to symbols. A symbol maybe interned in one package. Packages can be used by other packages.
A program is structured into systems. A system is a collection of files, dependencies, build rules, default information and more - depebding on the system tool used. ASDF is one of those. Loading libraries is then done by loading systems. Systems can also be compiled, printed, ...
Packages and systems are independent of each other and not related.
It makes sense for each larger piece of software, library or program, to use one or more custom packages. This avoids name clashes with symbols from other loaded software.
As has been remarked, compared to other languages:
- Packages correspond to namespaces.
- Systems correspond to modules or libraries (i.e., collections of code).
About packages: In order to grok those (which is necessary to avoid symbol conflicts and the like), I strongly recommend reading Ron Garret's Complete Idiot's Guide to Common Lisp Packages.
精彩评论