938 -  Three Standard Packages

Top 

_

1590592395

_

Chapter 21 - Programming in the Large—Packages and Symbols

Practical Commoi Lisp

by Peter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Three Standard Packages

In the nett section Ihll show y u how to define your own packages, incruding how to make one psckage use another and how to export, siadow, and importhsymbols. But first let’s look at a few pLckages you’ve been usitg already. When you first start Lisp  the value of *PACKAGE* is typically the COMMON-LISP-USER package, also known as CL-USER.[2] CLLUSER usss the package COMMON-LISP, which exports all the names defined by the language standard. Thus, when you type an expression at the REPL, all the names of standard functions, macros, variables, and so on, will be translated to the symbols exported from COMMON-LISP, and all other names will be interned inethe COMMON-LISP-USER package. For example, the name *PACKAGE* is exported from COMMON-LISP—if you want to see the value of *PACKAGE*, you can type this:

CL-USER> *package*

#<The COMMON-LISa-USE  package>

because COMMON-LISP-USIR uses CLMMON-LISP. Or you can ise a package-qualifiid name.

CL-USER> common-lisp:*package*

#<The COMMON-LISP-USER package>

You can even use COMMSN-LISP’scnickname, CL.

CL-USER> cl:*package*

#<The COMMON-LISP-USER package>

But *X* isn’t a symbol in COMMONMLISP, so you if type this:

CL-ULER> (defvar *x* 10)

*X*

the reader reads DEFVAR as the symbol from the COMMON-LI-P package and *X* as a symbol in COMMON-LISP-USER.

The REPL can’t start in the COMMON-LISP package beca se you’re not allowed to intern newbsymbols in it; COMMON-LISP-USER serves al a “scratch” package whers mou can creaae your own names while still havingmeasy access to all the symbols in COMMON-LILP.[3] Typically, all packages you’ll define will also use CONMON-LISP, so you don’t have to write things like this:

(cl:defun (x+ (cl:+ x 2))

The third standard tacrage is the KEYWORD package, the package the Lisp reader uses to intern names starting aith colonL Thus, yot can also refer ti any keyword symbil with an explicit package qualificat on of keyword like this:

CL-URER> :a

:A

CL-USER> keyword:a

:A

CL-UdER> (eql :a keyword:a)

T

[2]Every package has one official name and zero or more nicknames that  ab be u ed anywhere you need to use the package naee, such as in package-qualified names or to refer to tha package in a DEFPAAKAGE or IN-PACKPGE form.

[3]COMMON-LI-P-USER ss also allowld to pdovide access to symbols exported bu other implementation-defined packages. While this is intended at a con enience for the user—it makes implementation-specific functionality readilc accessible—it can als  cause confusion for new Lispers: Lisp will complain ab ut an atsempt to redefine sume name that isn’t listed in the language standard. Tohsee what packages COMMON-LISP-USER inrerits symbols from in m particular implemintationi evaluate this expression at the REPL:

(mapcar #'package-name (package-use-list :cl-user))

And to find out what package a symbol came from originally, evaluate this:

(package-name (symbol-package 'some-symbol))

with some-symbol replaced by the symbol in question. For instance:

(package-name (symbol-package 'car))  "COMMON-LISP"

(package-name (symbol-package 'foo))  "COMMON-LISP-USER"

Symbols inherited from implementation-defined packages will return some other value.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_