1、Basic Lisp TechniquesDavid J. Cooper, Jr.September 10, 2000ii0Copyright c 2000, Franz Inc. and David J. Cooper, Jr.Foreword1Computers, and the software applications that power them, permeate every facet of our daily lives.From groceries to airline reservations to dental appointments, our reliance on
2、 technology is all-encompassing.And, we want more. Every day, our expectations of technology and software increase:smaller cell phones that surf the netbetter search engines that generate information we actually wantvoice-activated laptopscars that know exactly where to goThe list is endless. Unfort
3、unately, there is not an endless supply of programmers and developersto satisfy our insatiable appetites for new features and gadgets. Cheap talent“ to help completethe grunt work“ of an application no longer exists. Further, the days of unlimited funding are over.Investors want to see results, fast
4、. Every day, hundreds of magazine and on-line articles focus onthe time and people resources needed to support future technological expectations.Common Lisp (CL) is one of the few languages and development options that can meet thesechallenges. Powerful, exible, changeable on the y | increasingly, C
5、L is playing a leading role inareas with complex problem-solving demands. Engineers in the elds of bioinformatics, scheduling,data mining, document management, B2B, and E-commerce have all turned to CL to complete theirapplications on time and within budget. But CL is no longer just appropriate for
6、the most complexproblems. Applications of modest complexity, but with demanding needs for fast development cyclesand customization, are also ideal candidates for CL.Other languages have tried to mimic CL, with limited success. Perl, Python, Java, C+, C# |they all incorporate some of the features tha
7、t give Lisp its power, but their implementations tendto be brittle.The purpose of this book is to showcase the features that make CL so much better than theseimitators, and to give you a quick-start“ guide for using Common Lisp as a development environ-ment. If you are an experienced programmer in l
8、anguages other than Lisp, this guide gives you allthe tools you need to begin writing Lisp applications. If youve had some exposure to Lisp in thepast, this guide will help refresh those memories and shed some new light on CL for you.But be careful, Lisp can be addicting! This is why many Fortune 50
9、0 companies will use nothingelse on their 24/7, cutting-edge, mission-critical applications. After reading this book, trying oursoftware, and experiencing a 3 to 10 times increase in productivity, we believe you will feel the sameway.1this Foreword was authored by Franz Inc.iiiivContents1 Introducti
10、on 11.1 The Past, Present, and Future of Common Lisp . . . . . . . . . . . . . . . . . . . . . 11.1.1 Lisp Yesterday . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1.2 Lisp Today . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1.3 Lisp Tomorrow
11、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 Convergence of Hardware and Software . . . . . . . . . . . . . . . . . . . . . . . . . 21.3 The CL Model of Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Operating a CL Development Environment 52.
12、1 Installing a CL Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Running CL in a Shell Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2.1 Starting CL from a Terminal Window . . . . . . . . . . . . . . . . . . . . . . 62.2.2 Stopping CL from a Te
13、rminal Window . . . . . . . . . . . . . . . . . . . . . . 62.3 Running CL inside a Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.3.1 A Note on Emacs and Text Editors . . . . . . . . . . . . . . . . . . . . . . . . 72.3.2 Emacs Terminology . . . . . . . . . . . . . . . . .
14、. . . . . . . . . . . . . . . 82.3.3 Starting, Stopping, and Working With CL inside an Emacs Shell . . . . . . . 82.4 Running CL as a subprocess of Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . 92.4.1 Starting the CL subprocess within Emacs . . . . . . . . . . . . . . . . . . . . 92.4.2
15、Working with CL as an Emacs subprocess . . . . . . . . . . . . . . . . . . . . 102.4.3 Compiling and Loading a File from an Emacs bu er . . . . . . . . . . . . . . 112.5 Integrated Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . 122.6 The User Init File . . . . . . . . .
16、. . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.7 Using CL as a scripting language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.8 Debugging in CL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.8.1 Common debugger commands . . . . . . . .
17、. . . . . . . . . . . . . . . . . . . 142.8.2 Interpreted vs Compiled Code . . . . . . . . . . . . . . . . . . . . . . . . . . 142.8.3 Use of (break) and C-c to interrupt . . . . . . . . . . . . . . . . . . . . . . . . 142.8.4 Pro ling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18、. . . . . . . 152.9 Developing Programs and Applications in CL . . . . . . . . . . . . . . . . . . . . . . 152.9.1 A Layered Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.9.2 Compiling and Loading your Project . . . . . . . . . . . . . . . . . . . . . . . 152.9.3 Creat
19、ing an Application Fasl“ File . . . . . . . . . . . . . . . . . . . . . . . 162.9.4 Creating an Image File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162.9.5 Building Runtime Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.9.6 Using an Application Init File . .
20、. . . . . . . . . . . . . . . . . . . . . . . . . 172.10 Using CL with Other Languages and Environments . . . . . . . . . . . . . . . . . . . 172.10.1 Interfacing with the Operating System . . . . . . . . . . . . . . . . . . . . . . 172.10.2 Foreign Function Interface . . . . . . . . . . . . . . . .
21、 . . . . . . . . . . . . . 18vvi CONTENTS2.10.3 Interfacing with Corba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182.10.4 Custom Socket Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182.10.5 Interfacing with Windows (COM, DLL, DDE) . . . . . . . . . . . . .
22、. . . . 202.10.6 Code Generation into Other Languages . . . . . . . . . . . . . . . . . . . . . 203 The CL Language 213.1 Overview of CL and its Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213.1.1 Evaluation of Arguments to a Function . . . . . . . . . . . . . . . . . . . . .
23、233.1.2 Lisp Syntax Simplicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.1.3 Turning O Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243.1.4 Fundamental CL Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 243.1.5 Functions . . . . . .
24、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263.1.6 Global and Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273.2 The List as a Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293.2.1 Accessing the Elements of a List . . . .
25、 . . . . . . . . . . . . . . . . . . . . . 293.2.2 The Rest“ of the Story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303.2.3 The Empty List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303.2.4 Are You a List? . . . . . . . . . . . . . . . . . . . . . . . . . .
26、 . . . . . . . . . 313.2.5 The Conditional If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313.2.6 Length of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.2.7 Member of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
27、3.2.8 Getting Part of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333.2.9 Appending Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333.2.10 Adding Elements to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.2.11 Removing Elemen
28、ts from a List . . . . . . . . . . . . . . . . . . . . . . . . . . 343.2.12 Sorting Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.2.13 Treating a List as a Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.2.14 Mapping a Function to a List . . .
29、. . . . . . . . . . . . . . . . . . . . . . . . 363.2.15 Property Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363.3 Control of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363.3.1 If . . . . . . . . . . . . . . . . . . . . . . . .
30、. . . . . . . . . . . . . . . . . . . 373.3.2 When . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.3.3 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.3.4 Cond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31、. . . . . . . . 383.3.5 Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.3.6 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.4 Functions as Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32、. . . 403.4.1 Named Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403.4.2 Functional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413.4.3 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413.4.4 Optional Argu
33、ments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423.4.5 Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423.5 Input, Output, Streams, and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 423.5.1 Read . . . . . . . . . . . . . . . .
34、 . . . . . . . . . . . . . . . . . . . . . . . . . 433.5.2 Print and Prin1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433.5.3 Princ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433.5.4 Format . . . . . . . . . . . . . . . . . . . . . . .
35、. . . . . . . . . . . . . . . . 443.5.5 Pathnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443.5.6 File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453.6 Hash Tables, Arrays, Structures, and Classes . . . . . . . . . . . . . . . . .
36、 . . . . . 463.6.1 Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46CONTENTS vii3.6.2 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.6.3 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37、 . . . 473.6.4 Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.7 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.7.1 Importing and Exporting Symbols . . . . . . . . . . . . . . . . . . . . . . . . 493.7.2 The
38、Keyword Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493.8 Common Stumbling Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493.8.1 Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493.8.2 Function Argument Lists .
39、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.8.3 Symbols vs. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.8.4 Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523.8.5 Distinguishing Macros from Functions . . . . . .
40、 . . . . . . . . . . . . . . . . 533.8.6 Operations that cons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544 CL as a CASE tool 554.1 Executable Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554.2 Knowledge Base . . . . . . . . . . . . . . . . . .
41、 . . . . . . . . . . . . . . . . . . . . 554.3 IDL and GDL Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564.3.1 Defpart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564.3.2 Making Instances and Sending Messages . . . . . . . . . . . .
42、. . . . . . . . . 574.3.3 Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584.3.4 Quanti ed Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594.4 Personal Accounting Application . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43、. 604.4.1 Persistent Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604.4.2 Adding Computed Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614.4.3 Building the Object Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . 624.4.4 Interacting with t
44、he Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675 CL as an Internet Application Server 695.1 AllegroServe and HTMLgen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
45、695.1.1 CL-based Webservers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695.1.2 Generating HTML with AllegroServes htmlgen . . . . . . . . . . . . . . . . . 705.2 GWL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715.2.1 Overview . . . . .
46、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715.2.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715.2.3 Separating the View“ from the Object . . . . . . . . . . . . . . . . . . . . . 735.3 Web-enabling the Personal Accounting Application
47、 . . . . . . . . . . . . . . . . . . . 735.3.1 Standard Database Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735.3.2 Customizing the Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765.3.3 Customizing the Root-level Page with a Report . . . . . . . . . . . . . .
48、 . . . 785.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81A Bibliography 83B Emacs Customization 85B.1 Lisp Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85B.2 Making Your Own Keychords . . . . . . . . . . .
49、 . . . . . . . . . . . . . . . . . . . . 85B.3 Keyboard Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86viii CONTENTSC Afterword 87C.1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87C.2 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87C.3 About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Index 89Chapter 1Introduction1.1 The Past, Present, and Future of Common Lisp1.1.1 Lisp YesterdayStanford University Professor Jo