Tải bản đầy đủ (.pdf) (1,096 trang)

IT training common lisp the language (2nd ed ) steele 1990 06 15

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (4.42 MB, 1,096 trang )

Contents

Preface (Second Edition) xi
Acknowledgments (Second Edition)
xiii
Acknowledgments (First Edition) xvii

1. Introduction
1.1.
1.2.
1.2.1.
1.2.2.
1.2.3.
1.2.4.
1.2.5.
1.2.6.
1.2.7.

Purpose 1
Notational Conventions 4
Decimal Numbers 4
Nil, False, and the Empty
List 4
Evaluation, Expansion, and
Equivalence 5
Errors 5
Descriptions of Functions and
Other Entities 6
The Lisp Reader 10
Overview of Syntax 10


2. Data Types
2.1.
2.1.1.
2.1.2.
2.1.3.
2.1.4.
2.2.
2.2.1.
2.2.2.
2.2.3.
2.2.4.
2.2.5.

1

13
Numbers 16
Integers 17
Ratios 19
Floating-Point Numbers 20
Complex Numbers 24
Characters 24
Standard Characters 25
Line Divisions 26
Non-standard Characters 27
Character Attributes 28
String Characters 28

2.3.
2.4.

2.5.
2.5.1.
2.5.2.
2.5.3.
2.6.
2.7.
2.8.
2.9.
2.10.
2.11.
2.12.
2.13.
2.14.
2.15.

Symbols 29
Lists and Conses 32
Arrays 33
Vectors 35
Strings 36
Bit-Vectors 37
Hash Tables 38
Readtables 38
Packages 38
Pathnames 38
Streams 38
Random-States 39
Structures 39
Functions 39
Unreadable Data Objects 40

Overlap, Inclusion, and
Disjointness of Types 41

3. Scope and Extent

46

4. Type Specifiers
4.1.
4.2.
4.3.
4.4.
4.5.
4.6.

53
Type Speci er Symbols 53
Type Speci er Lists 53
Predicating Type
Speci ers 55
Type Speci ers That
Combine 55
Type Speci ers That
Specialize 57
Type Speci ers That
Abbreviate 64
v


vi


CONTENTS

4.7.

De ning New Type
Speci ers 66
4.8. Type Conversion Function 68
4.9. Determining the Type of an
Object 70
4.10. Type Upgrading 72

5. Program Structure
5.1.
5.1.1.
5.1.2.
5.1.3.
5.1.4.
5.1.5.
5.2.
5.2.1.
5.2.2.
5.3.
5.3.1.
5.3.2.

73

Forms 73
Self-Evaluating Forms 74

Variables 74
Special Forms 76
Macros 78
Function Calls 79
Functions 79
Named Functions 80
Lambda-Expressions 80
Top-Level Forms 88
De ning Named Functions 90
Declaring Global Variables and
Named Constants 92
5.3.3. Control of Time of
Evaluation 94

6. Predicates

101
6.1. Logical Values 102
6.2. Data Type Predicates 102
6.2.1. General Type Predicates 102
6.2.2. Speci c Data Type
Predicates 105
6.3. Equality Predicates 110
6.4. Logical Operators 116

7. Control Structure

119
7.1. Constants and Variables 120
7.1.1. Reference 120

7.1.2. Assignment 127
7.2. Generalized Variables 129
7.3. Function Invocation 154
7.4. Simple Sequencing 155
7.5. Establishing New Variable
Bindings 157
7.6. Conditionals 165
7.7. Blocks and Exits 170

7.8.
7.8.1.
7.8.2.
7.8.3.

Iteration 172
Inde nite Iteration 172
General Iteration 173
Simple Iteration
Constructs 178
7.8.4. Mapping 180
7.8.5. The \Program Feature" 183
7.9. Structure Traversal and Side
E ects 188
7.10. Multiple Values 189
7.10.1. Constructs for Handling
Multiple Values 190
7.10.2. Rules Governing the Passing of
Multiple Values 195
7.11. Dynamic Non-Local Exits 198


8. Macros
8.1.
8.2.
8.3.
8.4.
8.5.

205
Macro De nition 206
Macro Expansion 215
Destructuring 217
Compiler Macros 217
Environments 220

9. Declarations
9.1.
9.2.
9.3.

228
Declaration Syntax 228
Declaration Speci ers 237
Type Declaration for
Forms 252

10. Symbols

254
10.1. The Property List 254
10.2. The Print Name 259

10.3. Creating Symbols 260

11. Packages
11.1.
11.2.
11.3.
11.4.
11.5.
11.6.

264
Consistency Rules 266
Package Names 266
Translating Strings to
Symbols 268
Exporting and Importing
Symbols 270
Name Con icts 272
Built-in Packages 275


CONTENTS

11.7. Package System Functions and
Variables 279
11.8. Modules 295
11.9. An Example 296

12. Numbers


307
12.1. Precision, Contagion, and
Coercion 308
12.2. Predicates on Numbers 312
12.3. Comparisons on Numbers 313
12.4. Arithmetic Operations 315
12.5. Irrational and Transcendental
Functions 319
12.5.1. Exponential and Logarithmic
Functions 320
12.5.2. Trigonometric and Related
Functions 323
12.5.3. Branch Cuts, Principal Values,
and Boundary Conditions in
the Complex Plane 332
12.6. Type Conversions and
Component Extractions on
Numbers 375
12.7. Logical Operations on
Numbers 382
12.8. Byte Manipulation
Functions 388
12.9. Random Numbers 391
12.10. Implementation
Parameters 394

13. Characters

398
13.1. Character Attributes 401

13.2. Predicates on Characters 403
13.3. Character Construction and
Selection 409
13.4. Character Conversions 410
13.5. Character Control-Bit
Functions 413

14. Sequences

415
14.1. Simple Sequence
Functions 419

vii

14.2. Concatenating, Mapping, and
Reducing Sequences 421
14.3. Modifying Sequences 426
14.4. Searching Sequences for
Items 432
14.5. Sorting and Merging 436

15. Lists
15.1.
15.2.
15.3.
15.4.
15.5.
15.6.


440
Conses 440
Lists 442
Alteration of List
Structure 453
Substitution of
Expressions 455
Using Lists as Sets 457
Association Lists 463

16. Hash Tables

468
16.1. Hash Table Functions 469
16.2. Primitive Hash Function 475

17. Arrays
17.1.
17.2.
17.3.
17.4.
17.5.
17.6.

476
Array Creation 476
Array Access 481
Array Information 482
Functions on Arrays of
Bits 487

Fill Pointers 489
Changing the Dimensions of an
Array 490

18. Strings

495
18.1. String Access 496
18.2. String Comparison 496
18.3. String Construction and
Manipulation 499

19. Structures

504
19.1. Introduction to
Structures 504
19.2. How to Use Defstruct 506


viii

CONTENTS

19.3. Using the Automatically
De ned Constructor
Function 510
19.4. Defstruct Slot-Options 512
19.5. Defstruct Options 513
19.6. By-Position Constructor

Functions 519
19.7. Structures of Explicitly
Speci ed Representational
Type 523
19.7.1. Unnamed Structures 523
19.7.2. Named Structures 524
19.7.3. Other Aspects of Explicitly
Speci ed Structures 526

20. The Evaluator

528
20.1. Run-Time Evaluation of
Forms 528
20.2. The Top-Level Loop 532

21. Streams

535
21.1. Standard Streams 535
21.2. Creating New Streams 538
21.3. Operations on Streams 542

22. Input/Output

547
22.1. Printed Representation of Lisp
Objects 547
22.1.1. What the Read Function
Accepts 548

22.1.2. Parsing of Numbers and
Symbols 554
22.1.3. Macro Characters 563
22.1.4. Standard Dispatching Macro
Character Syntax 570
22.1.5. The Readtable 582
22.1.6. What the Print Function
Produces 593
22.2. Input Functions 611
22.2.1. Input from Character
Streams 611
22.2.2. Input from Binary
Streams 622
22.3. Output Functions 622

22.3.1. Output to Character
Streams 623
22.3.2. Output to Binary
Streams 627
22.3.3. Formatted Output to
Character Streams 627
22.4. Querying the User 659

23. File System Interface

661
23.1. File Names 661
23.1.1. Pathnames 662
23.1.2. Case Conventions 668
23.1.3. Structured Directories 672

23.1.4. Extended Wildcards 675
23.1.5. Logical Pathnames 681
23.1.5.1. Syntax of Logical Pathname
Namestrings 681
23.1.5.2. Parsing of Logical Pathname
Namestrings 682
23.1.5.3. Using Logical
Pathnames 683
23.1.5.4. Examples of the Use of
Logical Pathnames 686
23.1.5.5. Discussion of Logical
Pathnames 688
23.1.6. Pathname Functions 690
23.2. Opening and Closing
Files 700
23.3. Renaming, Deleting, and Other
File Operations 707
23.4. Loading Files 712
23.5. Accessing Directories 718

24. Errors

720
24.1. General Error-Signaling
Functions 721
24.2. Specialized Error-Signaling
Forms and Macros 726
24.3. Special Forms for Exhaustive
Case Analysis 729


25. Miscellaneous Features

25.1. The Compiler 732
25.1.1. Compiler Diagnostics 740

732


CONTENTS

ix

25.1.2. Compiled Functions 741
25.1.3. Compilation Environment 742
25.1.4. Similarity of Constants 747
25.2. Documentation 751
25.3. Debugging Tools 752
25.4. Environment Inquiries 759
25.4.1. Time Functions 759
25.4.2. Other Environment
Inquiries 763
25.5. Identity Function 765

26. Loop

767
26.1. Introduction 767
26.2. How the Loop Facility
Works 767
26.3. Parsing Loop Clauses 768

26.3.1. Order of Execution 769
26.3.2. Kinds of Loop Clauses 770
26.3.3. Loop Syntax 773
26.4. User Extensibility 773
26.5. Loop Constructs 773
26.6. Iteration Control 774
26.7. End-Test Control 785
26.8. Value Accumulation 789
26.9. Variable Initializations 795
26.10. Conditional Execution 798
26.11. Unconditional Execution 800
26.12. Miscellaneous Features 802
26.12.1. Data Types 802
26.12.2. Destructuring 803

27. Pretty Printing

808

27.1. Introduction 808
27.2. Pretty Printing Control
Variables 809
27.3. Dynamic Control of the
Arrangement of Output 810
27.4. Format Directive
Interface 822
27.5. Compiling Format Control
Strings 825
27.6. Pretty Printing Dispatch
Tables 826


28. Common Lisp Object
System 831

28.1. Programmer Interface
Concepts 831
28.1.1. Error Terminology 832
28.1.2. Classes 835
28.1.2.1. De ning Classes 836
28.1.2.2. Creating Instances of
Classes 837
28.1.2.3. Slots 837
28.1.2.4. Accessing Slots 839
28.1.3. Inheritance 841
28.1.3.1. Inheritance of Methods 841
28.1.3.2. Inheritance of Slots and Slot
Options 841
28.1.3.3. Inheritance of Class
Options 843
28.1.3.4. Examples 843
28.1.4. Integrating Types and
Classes 843
28.1.5. Determining the Class
Precedence List 845
28.1.5.1. Topological Sorting 848
28.1.5.2. Examples 848
28.1.6. Generic Functions and
Methods 851
28.1.6.1. Introduction to Generic
Functions 851

28.1.6.2. Introduction to
Methods 852
28.1.6.3. Agreement on Parameter
Specializers and Quali ers 855
28.1.6.4. Congruent Lambda-Lists for
All Methods of a
Generic Function 856
28.1.6.5. Keyword Arguments in
Generic Functions and
Methods 856
28.1.7. Method Selection and
Combination 858
28.1.7.1. Determining the E ective
Method 858
28.1.7.2. Standard Method
Combination 861


x

CONTENTS

28.1.7.3. Declarative Method
Combination 863
28.1.7.4. Built-in Method Combination
Types 863
28.1.8. Meta-objects 865
28.1.8.1. Metaclasses 866
28.1.8.2. Standard Metaclasses 866
28.1.8.3. Standard Meta-objects 866

28.1.9. Object Creation and
Initialization 867
28.1.9.1. Initialization
Arguments 868
28.1.9.2. Declaring the Validity of
Initialization Arguments 869
28.1.9.3. Defaulting of Initialization
Arguments 870
28.1.9.4. Rules for Initialization
Arguments 871
28.1.9.5. Shared-Initialize 873
28.1.9.6. Initialize-Instance 874
28.1.9.7. De nitions of Make-Instance
and Initialize-Instance 876
28.1.10. Rede ning Classes 878
28.1.10.1. Modifying the Structure of
Instances 879
28.1.10.2. Initializing Newly Added
Local Slots 879
28.1.10.3. Customizing Class
Rede nition 880
28.1.10.4. Extensions 880
28.1.11. Changing the Class of an
Instance 881
28.1.11.1. Modifying the Structure of
an Instance 881
28.1.11.2. Initializing Newly Added
Local Slots 881
28.1.11.3. Customizing the Change of
Class of an Instance 882

28.1.12. Reinitializing an
Instance 882
28.1.12.1. Customizing
Reinitialization 884
28.2. Functions in the Programmer
Interface 884

29. Conditions

939
29.1. Introduction 939
29.2. Changes in Terminology 941
29.3. Survey of Concepts 942
29.3.1. Signaling Errors 942
29.3.2. Trapping Errors 944
29.3.3. Handling Conditions 946
29.3.4. Object-Oriented Basis of
Condition Handling 947
29.3.5. Restarts 949
29.3.6. Anonymous Restarts 949
29.3.7. Named Restarts 951
29.3.8. Restart Functions 952
29.3.9. Comparison of Restarts and
Catch/Throw 953
29.3.10. Generalized Restarts 955
29.3.11. Interactive Condition
Handling 956
29.3.12. Serious Conditions 956
29.3.13. Non-Serious Conditions 957
29.3.14. Condition Types 957

29.3.15. Signaling Conditions 958
29.3.16. Resignaling Conditions 959
29.3.17. Condition Handlers 959
29.3.18. Printing Conditions 960
29.4. Program Interface to the
Condition System 961
29.4.1. Signaling Conditions 961
29.4.2. Assertions 964
29.4.3. Exhaustive Case Analysis 968
29.4.4. Handling Conditions 970
29.4.5. De ning Conditions 974
29.4.6. Creating Conditions 977
29.4.7. Establishing Restarts 978
29.4.8. Finding and Manipulating
Restarts 987
29.4.9. Warnings 989
29.4.10. Restart Functions 990
29.4.11. Debugging Utilities 992
29.5. Prede ned Condition
Types 993

Appendix A. Series
A.1.

1001
Introduction 1001


CONTENTS


A.2.
A.2.1.
A.2.2.
A.2.3.
A.2.4.
A.2.5.
A.2.6.
A.3.
A.3.1.
A.3.2.
A.3.3.
A.3.4.
A.4.

xi

Series Functions 1003
Scanners 1004
Mapping 1008
Truncation and Other Simple
Transducers 1011
Conditional and Other
Complex Transducers 1013
Collectors 1017
Alteration of Series 1020
Optimization 1022
Basic Restrictions 1023
Constraint Cycles 1024
De ning New Series
Functions 1027

Declarations 1028
Primitives 1030

Appendix B. Generators and
Gatherers 1036
B.1.
B.2.
B.3.
B.4.

Introduction 1036
Generators 1037
Gatherers 1037
Discussion 1039

Appendix C. Backquote
References

1040

1053

Index of X3J13 Votes
Other Indexes

1064

1058



xii

CONTENTS


Preface

SECOND EDITION

Common Lisp has succeeded. Since publication of the rst edition of this
book in 1984, many implementors have used it as a de facto standard for Lisp
implementation. As a result, it is now much easier to port large Lisp programs
from one implementation to another. Common Lisp has proved to be a useful
and stable platform for rapid prototyping and systems delivery in arti cial
intelligence and other areas. With experience gained in using Common Lisp
for so many applications, implementors found no shortage of opportunities for
innovation. One of the important characteristics of Lisp is its good support for
experimental extension of the language while Common Lisp has been stable,
it has not stagnated.
The 1984 de nition of Common Lisp was imperfect and incomplete. In some
cases this was inadvertent: some odd boundary situation was overlooked and
its consequences not speci ed, or di erent passages were in con ict, or some
property of Lisp was so well-known and traditionally relied upon that I forgot
to write it down. In other cases the informal committee that was de ning
Common Lisp could not settle on a solution, and therefore agreed to leave
some important aspect of the language unspeci ed rather than choose a less
than satisfactory de nition. An example is error handling 1984 Common
Lisp had plenty of ways to signal errors but no way for a program to trap or
process them.
Over the next year I collected reports of errors in the book and gaps in

the language. In December 1985, a group of implementors and users met in
Boston to discuss the state of Common Lisp. I prepared two lists for this
meeting, one of errata and clari cations that I thought would be relatively
uncontroversial (boy, was I wrong!) and one of more substantial changes I
thought should be considered and perhaps voted upon. Others also brought
proposals to discuss. It became clear to everyone that there was now enough
interest in Common Lisp, and dependence on its stability, that a more formal
mechanism was needed for managing changes to the language.
xiii


xiv

PREFACE (SECOND EDITION)

This realization led to the formation of X3J13, a subcommittee of ANSI
committee X3, to produce a formal American National Standard for Common
Lisp. That process is nearing completion. X3J13 has completed the bulk of
its technical work in rectifying the 1984 de nition and codifying extensions
to that de nition that have received widespread use and approval. A draft
standard is now being prepared it will probably be available in 1990. There
will then be a period (required by ANSI) for public review. X3J13 must then
consider the comments it receives and respond appropriately. If the comments
result in substantial changes to the draft standard, multiple public review
periods may be required before the draft can be approved as an American
National Standard.
Fortunately, X3J13 has done an outstanding job of documenting its work.
For every change that came to a formal vote, a document was prepared that
described the problem to be solved and one or more solutions. For each
solution there is a detailed proposal for changing the language a rationale test

cases that distinguish the proposal from the status quo or from other proposals
for solving that problem discussions of current practice, cost to implementors,
cost to users, cost of not adopting the proposal, bene ts of adoption, aesthetic
criteria and any relevant informal discussion that may have preceded creation
of the formal proposal. All of these proposal documents were made available
on-line as well as in paper form. By my count, by June 1989 some 186 such
proposals were approved as language changes. (This count does not include
many proposals that came before the committee but were rejected.)
The purpose of this second edition is to bridge the gap between the rst
edition and the forthcoming ANSI standard for Common Lisp. Because of the
requirement for formal public review, it will be some time yet before the ANSI
standard is nal. This book in no way resembles the forthcoming standard
(which is being written independently by Kathy Chapman of Digital Equipment Corporation with assistance from the X3J13 Drafting Subcommittee).
I have incorporated into this second edition a great deal of material based
on the votes of X3J13, in order to give the reader a picture of where the
language is heading. My purpose here is not simply to quote the X3J13
documents verbatim but to paraphrase them and relate them to the structure
of the rst edition. A single vote by X3J13 may be discussed in many parts
of this book, and a single passage of this book may be a ected by many of
the votes.
I wish to be very clear: this book is not an o cial document of X3J13,
though it is based on publicly available material produced by X3J13. In
no way does this book constitute a de nitive description of the forthcoming
ANSI standard. The committee's decisions have been remarkably stable (it


PREFACE (SECOND EDITION)

xv


has rescinded earlier decisions only two or three times), and I do not expect
radical changes in direction. Nevertheless, it is quite probable that the draft
standard will be substantively revised in response to editorial review or public
comment. I have therefore reported here on the actions of X3J13 not to
inscribe them in stone, but to make clear how the language of the rst edition
is likely to change. I have tried to be careful in my wording to avoid saying
\the language has been changed" and to state simply that \X3J13 voted at
such-and-so time to make the following change."
Until the day when an o cial ANSI Common Lisp standard emerges, it
is likely that the 1984 de nition of Common Lisp will continue to be used
widely. This book has been designed to be used as a reference both to the
1984 de nition and to the language as modi ed by the actions of X3J13.
It contains the entire text of the rst edition of Common Lisp: The Language, with corrections and minor editorial changes however, more than half
of the material in this edition is new. All new material is identi ed by solid
lines in the left margin. Dotted lines in the left margin indicate material from
the rst edition that applies to the 1984 de nition but that has been modi ed
by a vote of X3J13. Modi cations to these outmoded passages are explained
by preceding or following text (which will have a solid line in the margin). In
summary:
. To use the 1984 language de nition, read all material that does not have a
solid line in the margin.
. To use the updated language de nition, read everything, but be wary of
material with a dotted line in the margin.
At the end of the book is an index of the X3J13 votes, ordered by the committee's internal code names (included to ease cross-reference to the X3J13
documents, which may be useful during the public review periods). References to this list of votes appear as numbers in angle brackets thus \h14i"
refers to the vote on issue number 14, whereas \ 14]" refers to reference 14 in
the bibliography.
I have kept changes to the wording of the rst-edition material to a minimum. Obvious spelling and typographical errors have been corrected, and
the entire text has been edited to a uniform style of spelling and punctuation.
(Note in particular that the rst edition used the spelling \signalling" but

this edition, in deference to the style decision of the X3J13 Drafting Subcommittee, uses \signaling.") A few minor changes were made to accommodate
typographical or layout constraints. (For example, the word \also" has been
deleted from the rst sentence of chapter 1, partly to make that paragraph
look better and partly to allow a better page break at the bottom of page 2.)


xvi

PREFACE (SECOND EDITION)

In a very few cases the rst edition contained substantive errors that I could
not in good conscience correct silently these have been agged by paragraphs
beginning with the phrase Notice of correction.
The chapter and section numbering of this edition matches that of the
rst edition, with the exception that a new section 7.9 has been interpolated.
Four new chapters (26{29) describe substantial changes approved by X3J13:
an extended loop macro, a pretty printer interface, the Common Lisp Object
System, and the Common Lisp Condition System.
X3J13, in the course of its work, formed a subcommittee to study whether
additional means of iteration should be standardized for use in Common Lisp,
for a great deal of existing practice in this area was not included in the rst
edition because of lack of agreement in 1984. The X3J13 Iteration Subcommittee produced reports on three possible facilities. One (loop) was approved
for inclusion in the forthcoming draft standard and is described in chapter 26.
X3J13 expressed interest in the other two approaches (series and generators), but the consensus as of January 1989 was that these other approaches
were not yet su ciently mature or in su ciently widespread use to warrant
inclusion in the draft Common Lisp standard at that time. However, the subcommittee was directed to continue work on these approaches and X3J13 is
open to the possibility of standardizing them at a later date. Please note that
I do not wish the prejudge the question of whether X3J13 will ever choose
to make the other two proposals the subject of standardization. Nevertheless, I have chosen to include them in the second edition, in cooperation with
Dr. Richard C. Waters, as appendices A and B, in order to make these ideas

available to the Lisp community. In my judgement these proposals address
an area of language design not otherwise covered by Common Lisp and are
likely to have practical value even if they are never adopted as part of a formal
standard.
Some new material in this book has nothing to do with the work of X3J13.
In many places I have added explanations, clari cations, new examples, warnings, and tips on writing portable code. Appendix C contains a piece of code
that may help in understanding the backquote syntax.
This second edition, unlike the rst edition, also includes a few diagrams
to pep up the text. However, there are absolutely no new jokes, and very few
outright lies.


Acknowledgments
SECOND EDITION

First and foremost, I must thank the many people in the Lisp community who
have worked so hard to specify, implement, and use Common Lisp. Some of
these have volunteered many hours of e ort as members of ANSI committee
X3J13. Others have made presentations or proposals to X3J13, and yet others
have sent suggestions and corrections to the rst edition directly to me. This
book builds on their e orts as well as mine.
An early draft of this book was made available to all members of X3J13 for
their criticism. I have also worked with the many public documents that have
been written during the course of the committee's work (which is not over
yet). It is my hope that this book is an accurate re ection of the committee's
actions as of October 1989. Nevertheless, any errors or inconsistencies are my
responsibility. The fact that I have made a draft available to certain persons,
received feedback from them, or thanked them in these acknowledgments does
not necessarily imply that any one of them or any of the institutions with
which they are a liated endorse this book or anything of its contents.

Digital Press and I gave permission to X3J13 to use any or all parts of
the rst edition in the production of an ANSI Common Lisp standard. Conversely, in writing this book I have worked with publicly available documents
produced by X3J13 in the course of its work, and in some cases as a courtesy
have obtained the consent of the authors of those documents to quote them extensively. This common ancestry will result in similarities between this book
and the emerging ANSI Common Lisp standard (that is the purpose, after
all). Nevertheless, this second edition has no o cial connection whatsoever
with X3J13 or ANSI, nor is it endorsed by either of those institutions.
The following persons have been members of X3J13 or involved in its activities at one time or another: Jim Allard, Dave Andre, Jim Antonisse,
William Arbaugh, John Aspinall, Bob Balzer, Gerald Barber, Richard Barber, Kim Barrett, David Bartley, Roger Bate, Alan Bawden, Michael Beckerle,
Paul Beiser, Eric Benson, Daniel Bobrow, Mary Boelk, Skona Brittain, Gary
xvii


xviii

ACKNOWLEDGMENTS (SECOND EDITION)

Brown, Tom Bucken, Robert Buckley, Gary Byers, Dan Carnese, Bob Cassels,
Jer^ome Chailloux, Kathy Chapman, Thomas Christaller, Will Clinger, Peter
Co ee, John Cugini, Pavel Curtis, Doug Cutting, Christopher Dabrowski,
Je Dalton, Linda DeMichiel, Fred Discenzo, Jerry Duggan, Patrick Dussud,
Susan Ennis, Scott Fahlman, Jogn Fitch, John Foderaro, Richard Gabriel,
Steven Gadol, Nick Gall, Oscar Garcia, Robert Giansiracusa, Brad Goldstein, David Gray, Richard Greenblatt, George Hadden, Steve Ha ich, Dave
Henderson, Carl Hewitt, Carl Ho man, Cheng Hu, Masayuki Ida, Takayasu
Ito, Sonya Keene, James Kempf, Gregory Jennings, Robert Kerns, Gregor
Kiczales, Kerry Kimbrough, Dieter Kolb, Timothy Koschmann, Ed Krall,
Fritz Kunze, Aaron Larson, Joachim Laubsch, Kevin Layer, Michael Levin,
Ray Lim, Thom Linden, David Loe er, Sandra Loosemore, Barry Margolin,
Larry Masinter, David Matthews, Robert Mathis, John McCarthy, Chris McConnell, Rob McLachlan, Jay Mendelsohn, Martin Mikelsons, Tracey Miles,
Richard Mlyarnik, David Moon, Jarl Nilsson, Leo Noordhulsen, Ronald Ohlander, Julian Padget, Je Peck, Jan Pedersen, Bob Pellegrino, Crispin Perdue, Dan Pierson, Kent Pitman, Dexter Pratt, Christian Quiennec, B. Raghavan, Douglas Rand, Jonathan Rees, Chris Richardson, Je Rininger, Walter

van Roggen, Je rey Rosenking, Don Sakahara, William Scherlis, David Slater,
James Smith, Alan Snyder, Angela Sodan, Richard Soley, S. Sridhar, Bill
St. Clair, Philip Stanhope, Guy Steele, Herbert Stoyan, Hiroshi Torii, Dave
Touretzky, Paul Tucker, Rick Tucker, Thomas Turba, David Unietis, Mary
Van Deusen, Ellen Waldrum, Richard Waters, Allen Wechsler, Mark Wegman, Jon L White, Skef Wholey, Alexis Wieland, Martin Yonke, Bill York,
Taiichi Yuasa, Gail Zacharias, and Jan Zubko .
I must express particular gratitude and appreciation to a number of people
for their exceptional e orts:
Larry Masinter, chairman of the X3J13 Cleanup Subcommittee, developed
the standard format for documenting all proposals to be voted upon. The
result has been an outstanding tehcnical and historical record of all the actions
taken by X3J13 to rectify and improve Common Lisp.
Sandra Loosemore, chairwoman of the X3J13 Compiler Subcommittee, produced many proposals for clarifying the semantics of the compilation process.
She has been a diligent stickler for detail and has helped to clarify many parts
of Common Lisp left vague in the rst edition.
Jon L White, chairman of the X3J13 Iteration Subcommittee, supervised
the consideration of several controversial proposals, one of which (loop) was
eventually adopted by X3J13.
Thom Linden, chairman of the X3J13 Character Subcommittee, led a team
in grappling with the di cult problem of accommodating various character


ACKNOWLEDGMENTS (SECOND EDITION)

xix

sets in Common Lisp. One result is that Common Lisp will be more attractive
for international use.
Kent Pitman, chairman of the X3J13 Error Handling Subcommittee,
plugged the biggest outstanding hole in Common Lisp as described by the

rst edition.
Kathy Chapman, chairwoman of the X3J13 Drafting Subcommittee, and
principal author of the draft standard, has not only written a great deal of
text but also insisted on coherent and consistent terminology and pushed the
rest of the committee forward when necessary.
Robert Mathis, chairman of X3J13, has kept administrative matters owing
smoothly during technical controversies.
Mary Van Deusen, secretary of X3J13, kept excellent minutes that were a
tremendous aid to me in tracing the history of a number of complex discussions.
Jan Zubko , X3J13 meeting and mailing organizer, knows what's going
on, as always. She is a master of organization and of physical arrangements.
Moreover, she once again pulled me out of the re at the last minute.
Dick Gabriel, international representative for X3J13, has kept information
owing smoothly between Europe, Japan, and the United States. He provided
a great deal of the energy and drive for the completion of the Common Lisp
Object System speci cation. He has also provided me with a great deal of
valuable advice and has been on call for last-minute consultation at all hours
during the nal stages of preparation for this book.
David Moon has consistently been a source of reason, expert knowledge,
and careful scrutiny. He has read the rst edition and the X3J13 proposals
perhaps more carefully than anyone else.
David Moon, Jon L White, Gregor Kiczales, Robert Mathis, Mary Boelk
provided extensive feedback on an early draft of this book. I thank them as
well as the many others who commented in one way or another on the draft.
I wish to thank the authors of large proposals to X3J13 that have made
material available for more or less wholesale inclusion in this book as distinct
chapters. This material was produced primarily for the use of X3J13 in its
work. It has been included here on a non-exclusive basis with the consent of
the authors.
The author of the chapter on loop (Jon L White) notes that the chapter

is based on documentation written at Lucid, Inc., by Molly M. Miller, Sonia
Orin Lyris, and Kris Dinkel. Glenn Burke, Scott Fahlman, Colin Meldrum,
David Moon, Cris Perdue, and Dick Waters contributed to the design of the
loop macro.
The authors of the Common Lisp Object System speci cation (Daniel


xx

ACKNOWLEDGMENTS (SECOND EDITION)

G. Bobrow, Linda G. DeMichiel, Richard P. Gabriel, Sonya E. Keene, Gregor
Kiczales, and David A. Moon) wish to thank Patrick Dussud, Kenneth Kahn,
Jim Kempf, Larry Masinter, Mark Ste k, Daniel L. Weinreb, and Jon L White
for their contributions.
The author of the chapter on Conditions (Kent M. Pitman) notes that there
is a paper 38] containing background information about the design of the condition system, which is based on the condition system of the Symbolics Lisp
Machines 49]. The members of the X3J13 Error Handling Subcommittee were
Andy Daniels and Kent Pitman. Richard Mlynarik and David A. Moon made
major design contributions. Useful comments, questions, suggestions, and
criticisms were provided by Paul Anagnostopoulos, Alan Bawden, William
Chiles, Pavel Curtis, Mary Fontana, Dick Gabriel, Dick King, Susan Lander,
David D. Loe er, Ken Olum, David C. Plummer, Alan Snyder, Eric Weaver,
and Daniel L. Weinreb. The Condition System was designed speci cally to accommodate the needs of Common Lisp. The design is, however, most directly
based on the \New Error System" (NES) developed at Symbolics by David L.
Andre, Bernard S. Greenberg, Mike McMahon, David A. Moon, and Daniel L.
Weinreb. The NES was in turn based on experiences with the original Lisp
Machine error system (developed at MIT), which was found to be inadequate
for the needs of the modern Lisp Machine environments. Many aspects of
the NES were inspired by the (PL/I) condition system used by the Honeywell

Multics operating system. Henry Lieberman provided conceptual guidance
and encouragement in the design of the NES. A reimplementation of the NES
for non-Symbolics Lisp Machine dialects (MIT, LMI, and TI) was done at
MIT by Richard M. Stallman. During the process of that reimplementation,
some conceptual changes were made which have signi cantly in uenced the
Common Lisp Condition System.
As for the smaller but no less important proposals, Larry Masinter deserves
recognition as an author of over half of them. He has worked indefatigably to
write up proposals and to polish drafts by other authors. Kent Pitman, David
Moon, and Sandra Loosemore have also been notably proli c, as well as Jon L
White, Dan Pierson, Walter van Roggen, Skona Brittain, Scott Fahlman, and
myself. Other authors of proposals include David Andre, John Aspinall, Kim
Barrett, Eric Benson, Daniel Bobrow, Bob Cassels, Kathy Chapman, WIlliam
Clinger, Pavel Curtis, Doug Cutting, Je Dalton, Linda DiMichiel, Richard
Gabriel, Steven Ha ich, Sonya Keene, James Kempf, Gregor Kiczales, Dieter
Kolb, Barry Margolin, Chris McConnell, Je Peck, Jan Pedersen, Crispin
Perdue, Jonathan Rees, Don Sakahara, David Touretzky, Richard Waters,
and Gail Zacharias.
I am grateful to Donald E. Knuth and his colleagues for producing the


ACKNOWLEDGMENTS (SECOND EDITION)

xxi

TEX text formatting system 28], which was used to produce and typeset the
manuscript. Knuth did an especially good job of publishing the program
for TEX 29] I had to consult the code about eight times while debugging
particularly complicated macros. Thanks to the extensive indexing and crossreferences, in each case it took me less than ve minutes to nd the relevant
fragment of that 500-page program.

I also owe a debt to Leslie Lamport, author of the LaTEX macro package 30]
for TEX, within which I implemented the document style for this book.
Blue Sky Research sells and supports Textures, an implementation of TEX
for Apple Macintosh computers Gayla Groom and Barry Smith of Blue Sky
Research provided excellent technical support when I needed it. Other software tools that were invaluable in preparing this book were QuicKeys (sold
by CE Software, Inc.), which provides keyboard macros Gofer (sold by Microlytics, Inc.), which performs rapid text searches in multiple les Symantec
Utilities for Macintosh (sold by Symantec Corporation), which saved me from
more than one disk crash and the PostScript language and compatible fonts
(sold by Adobe Systems Incorporated).
Some of this software (such as LaTEX) I obtained for free and some I bought,
but all have proved to be useful tools of excellent quality. I am grateful to
these developers for creating them.
Electronic mail has been indispensible to the writing of this book, as well
to as the work of X3J13. (It is a humbling experience to publish a book and
then for the next ve years to receive at least one electronic mail message a
week, if not twenty, pointing out some mistake or defect.) Kudos to those
develop and maintain the Internet, which arose from the Arpanet and other
networks.
Chase Du y, George Horesta, and Will Buddenhagen of Digital Press have
given me much encouragement and support. David Ford designed the book
and provided speci cations that I could code into TEX. Alice Cheyer and Kate
Schmit edited the copy for style and puzzled over the more obscure jokes with
great patience. Marilyn Rowland created the index Tim Evans and I did
some polishing. Laura Fillmore and her colleagues at Editorial, Inc., have
tirelessly and meticulously checked one draft after another and has kept the
paperwork owing smoothly during the last hectic weeks of proofreading, page
makeup, and typesetting.
Thinking Machines Corporation has supported all my work with X3J13. I
thank all my colleagues there for their encouragement and help.
Others who provided indispensible encouragement and support include Guy

and Nalora Steele David Steele Cordon and Ruth Kerns David, Patricia,
Tavis, Jacob, Nicholas, and Daniel Auwerda Donald and Denise Kerns and


xxii

ACKNOWLEDGMENTS (SECOND EDITION)

David, Joyce, and Christine Kerns.
Most of the writing of this book took place between 10 P.M. and 3 A.M. (I'm
not as young as I used to be). I am grateful to Barbara, Julia, Peter, and
Matthew for putting up with it, and for their love.
Guy L. Steele Jr.
Lexington, Massachusetts
All Saints' Day, 1989


Acknowledgments
FIRST EDITION (1984)

Common Lisp was designed by a diverse group of people a liated with many
institutions.
Contributors to the design and implementation of Common Lisp and to the
polishing of this book are hereby gratefully acknowledged:
Paul Anagnostopoulos Digital Equipment Corporation
Dan Aronson
Carnegie-Mellon University
Alan Bawden
Massachusetts Institute of Technology
Eric Benson

University of Utah, Stanford University, and Symbolics,
Incorporated
Jon Bentley
Carnegie-Mellon University and Bell Laboratories
Jerry Boetje
Digital Equipment Corporation
Gary Brooks
Texas Instruments
Rodney A. Brooks Stanford University
Gary L. Brown
Digital Equipment Corporation
Richard L. Bryan
Symbolics, Incorporated
Glenn S. Burke
Massachusetts Institute of Technology
Howard I. Cannon Symbolics, Incorporated
George J. Carrette Massachusetts Institute of Technology
Robert Cassels
Symbolics, Incorporated
Monica Cellio
Carnegie-Mellon University
David Dill
Carnegie-Mellon University
Scott E. Fahlman
Carnegie-Mellon University
Richard J. Fateman University of California, Berkeley
Neal Feinberg
Carnegie-Mellon University
Ron Fischer
Rutgers University

John Foderaro
University of California, Berkeley
Steve Ford
Texas Instruments
xxiii


xxiv

ACKNOWLEDGMENTS (FIRST EDITION, 1984)

Richard P. Gabriel

Stanford University and Lawrence Livermore National
Laboratory
Joseph Ginder
Carnegie-Mellon University and Perq Systems Corp.
Bernard S. Greenberg Symbolics, Incorporated
Richard Greenblatt Lisp Machines Incorporated (LMI)
Martin L. Griss
University of Utah and Hewlett-Packard Incorporated
Steven Handerson
Carnegie-Mellon University
Charles L. Hedrick Rutgers University
Gail Kaiser
Carnegie-Mellon University
Earl A. Killian
Lawrence Livermore National Laboratory
Steve Krueger
Texas Instruments

John L. Kulp
Symbolics, Incorporated
Jim Large
Carnegie-Mellon University
Rob Maclachlan
Carnegie-Mellon University
William Maddox
Carnegie-Mellon University
Larry M. Masinter Xerox Corporation, Palo Alto Research Center
John McCarthy
Stanford University
Michael E. McMahon Symbolics, Incorporated
Brian Milnes
Carnegie-Mellon University
David A. Moon
Symbolics, Incorporated
Beryl Morrison
Digital Equipment Corporation
Don Morrison
University of Utah
Dan Pierson
Digital Equipment Corporation
Kent M. Pitman
Massachusetts Institute of Technology
Jonathan Rees
Yale University
Walter van Roggen Digital Equipment Corporation
Susan Rosenbaum
Texas Instruments
William L. Scherlis Carnegie-Mellon University

Lee Schumacher
Carnegie-Mellon University
Richard M. Stallman Massachusetts Institute of Technology
Barbara K. Steele
Carnegie-Mellon University
Guy L. Steele Jr.
Carnegie-Mellon University and Tartan Laboratories
Incorporated
Peter Szolovits
Massachusetts Institute of Technology
William vanMelle
Xerox Corporation, Palo Alto Research Center
Ellen Waldrum
Texas Instruments
Allan C. Wechsler
Symbolics, Incorporated
Daniel L. Weinreb
Symbolics, Incorporated
Jon L White
Xerox Corporation, Palo Alto Research Center
Skef Wholey
Carnegie-Mellon University


ACKNOWLEDGMENTS (FIRST EDITION, 1984)

xxv

Richard Zippel
Leonard Zubko


Massachusetts Institute of Technology
Carnegie-Mellon University and Tartan Laboratories
Incorporated
Some contributions were relatively small others involved enormous expenditures of e ort and great dedication. A few of the contributors served more as
worthy adversaries than as benefactors (and do not necessarily endorse the nal design reported here), but their pointed criticisms were just as important
to the polishing of Common Lisp as all the positively phrased suggestions.
All of the people named above were helpful in one way or another, and I am
grateful for the interest and spirit of cooperation that allowed most decisions
to be made by consensus after due discussion.
Considerable encouragement and moral support were also provided by:
Norma Abel
Roger Bate
Harvey Cragon
Dennis Duncan
Sam Fuller
A. Nico Habermann
Berthold K. P. Horn
Gene Kromer
Gene Matthews
Allan Newell
Dana Scott
Harry Tennant
Patrick H. Winston
Lowell Wood
William A. Wulf

Digital Equipment Corporation
Texas Instruments
Texas Instruments

Digital Equipment Corporation
Digital Equipment Corporation
Carnegie-Mellon University
Massachusetts Institute of Technology
Texas Instruments
Texas Instruments
Carnegie-Mellon University
Carnegie-Mellon University
Texas Instruments
Massachusetts Institute of Technology
Lawrence Livermore National Laboratory
Carnegie-Mellon University and Tartan Laboratories
Incorporated
I am very grateful to each of them.
Jan Zubko of Carnegie-Mellon University provided a great deal of organization, secretarial support, and unfailing good cheer in the face of adversity.
The development of Common Lisp would most probably not have been
possible without the electronic message system provided by the ARPANET.
Design decisions were made on several hundred distinct points, for the most
part by consensus, and by simple majority vote when necessary. Except for
two one-day face-to-face meetings, all of the language design and discussion
was done through the ARPANET message system, which permitted e ortless
dissemination of messages to dozens of people, and several interchanges per


xxvi

ACKNOWLEDGMENTS (FIRST EDITION, 1984)

day. The message system also provided automatic archiving of the entire
discussion, which has proved invaluable in the preparation of this reference

manual. Over the course of thirty months, approximately 3000 messages
were sent (an average of three per day), ranging in length from one line to
twenty pages. Assuming 5000 characters per printed page of text, the entire
discussion totaled about 1100 pages. It would have been substantially more
di cult to have conducted this discussion by any other means, and would
have required much more time.
The ideas in Common Lisp have come from many sources and been polished
by much discussion. I am responsible for the form of this book, and for any
errors or inconsistencies that may remain but the credit for the design and
support of Common Lisp lies with the individuals named above, each of whom
has made signi cant contributions.
The organization and content of this book were inspired in large part by
the MacLISP Reference Manual by David A. Moon and others 33], and by
the LISP Machine Manual (fourth edition) by Daniel Weinreb and David
Moon 55], which in turn acknowledges the e orts of Richard Stallman, Mike
McMahon, Alan Bawden, Glenn Burke, and \many people too numerous to
list."
I thank Phyllis Keenan, Chase Du y, Virginia Anderson, John Osborn,
and Jonathan Baker of Digital Press for their help in preparing this book
for publication. Jane Blake did an admirable job of copy-editing. James
Gibson and Katherine Downs of Waldman Graphics were most cooperative in
typesetting this book from my on-line manuscript les.
I am grateful to Carnegie-Mellon University and to Tartan Laboratories
Incorporated for supporting me in the writing of this book over the last three
years.
Part of the work on this book was done in conjunction with the CarnegieMellon University Spice Project, an e ort to construct an advanced scienti c
software development environment for personal computers. The Spice Project
is supported by the Defense Advanced Research Projects Agency, Department
of Defense, ARPA Order 3597, monitored by the Air Force Avionics Laboratory under contract F33615-78-C-1551. The views and conclusions contained
in this book are those of the author and should not be interpreted as representing the o cial policies, either expressed or implied, of the Defense Advanced

Research Projects Agency or the U.S. Government.
Most of the writing of this book took place between midnight and 5 A.M. I
am grateful to Barbara, Julia, and Peter for putting up with it, and for their
love.


ACKNOWLEDGMENTS (FIRST EDITION, 1984)

Guy L. Steele Jr.
Pittsburgh, Pennsylvania
March 1984

xxvii


Would it be wonderful if, under the
pressure of all these di culties, the
Convention should have been
forced into some deviations from
that arti - cial structure and
regular symmetry which an
abstract view of the subject might
lead an ingenious theorist to
bestow on a constitution planned
in his closet or in his imagination?
|James Madison, The Federalist
No. 37, January 11, 1788


1


Introduction
Common Lisp is a new dialect of Lisp, a successor to MacLisp 33, 37], inuenced strongly by Zetalisp 55, 34] and to some extent by Scheme 46] and
Interlisp 50].

1.1. Purpose

Common Lisp is intended to meet these goals:
Commonality

Common Lisp originated in an attempt to focus the work of several implementation groups, each of which was constructing successor implementations
of MacLisp for di erent computers. These implementations had begun to
diverge because of the di erences in the implementation environments: microcoded personal computers (Zetalisp, Spice Lisp), commercial timeshared
computers (NIL|the \New Implementation of Lisp"), and supercomputers
(S-1 Lisp). While the di erences among the several implementation environments of necessity will continue to force certain incompatibilities among the
implementations, Common Lisp serves as a common dialect to which each
implementation makes any necessary extensions.
Portability

Common Lisp intentionally excludes features that cannot be implemented
easily on a broad class of machines. On the one hand, features that are
di cult or expensive to implement on hardware without special microcode
are avoided or provided in a more abstract and e ciently implementable
form. (Examples of this are the invisible forwarding pointers and locatives
of Zetalisp. Some of the problems that they solve are addressed in di erent ways in Common Lisp.) On the other hand, features that are useful
1


×