ANSI Common Lisp PRENTICE HALL SERIES UW§ IN ARTIFICIAL INTELLIGENCE Stuart Russell and Peter Norvig, Editors GRAHAM ANSI Common Lisp MUGGLETON Logical Foundations of Machine Learning RUSSELL & NORVIG Artificial Intelligence: A Modern Approach ANSI Common Lisp Paul Graham An Alan R. Apt Book Prentice Hall, Upper Saddle River, New Jersey 07458 Library of Congress Cataloging-in-Publication Data Graham, Paul. ANSI common lisp. / Paul Graham. p. cm. "An Alan R. Apt book." Includes bibliographical references and index. ISBN 0-13-370875-6 1. COMMON LISP (Computer program language) I. Tide. QA76.73.C28G69 1996 005.13'3-dc20 95-45017 CIP Publisher: Alan Apt Production Editor: Mona Pompili Cover Designer: Gino Lee Copy Editor: Shirley Michaels Production Coordinator: Donna Sullivan Editorial Assistant: Shirley McGuire Cover Photo: Ed Lynch •m © 1996 by Prentice Hall, Inc. Upper Saddle River, NJ 07458 The author and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The author and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs. All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher. All trademarks are the property of their respective owners. Printed in the United States of America 20 ISBN 0-13-370875-6 Prentice-Hall International (UK) Limited, London Prentice-Hall of Australia Pty. Limited, Sydney Prentice-Hall of Canada, Inc., Toronto Prentice-Hall Hispanoamericana, S. A., Mexico Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Prentice-Hall Asia Pte. Ltd., Singapore Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro TORTM Half lost on my firmness gains to more glad heart, Or violent and from forage drives A glimmering of all sun new begun Both harp thy discourse they march'd, Forth my early, is not without delay; For their soft with whirlwind; and balm. Undoubtedly he scornful turn'd round ninefold, Though doubled now what redounds, And chains these a lower world devote, yet inflicted? Till body or rare, and best things else enjoy'd in heav'n To stand divided light at ev'n and poise their eyes, Or nourish, lik'ning spiritual, I have thou appear. —Henley Preface The aim of this book is to teach you Common Lisp quickly and thoroughly. It is really two books. The first half is a tutorial that explains, with plenty of examples, all the essential concepts of Lisp programming. The second half is an up-to-date summary of ANSI Common Lisp, describing every operator in the language. Audience ANSI Common Lisp is intended for both students and professional program mers. It assumes no prior knowledge of Lisp. Experience writing programs in some other language would be helpful, but not absolutely necessary. The book begins with the most basic concepts, and pays special attention to the points that tend to confuse someone seeing Lisp for the first time. This book could be used by itself as the textbook in a course on Lisp programming, or to teach Lisp as part of a course on artificial intelligence or programming languages. Professional programmers who want to learn Lisp will appreciate the direct, practical approach. Those who already use Lisp will find it a useful source of examples, and a convenient reference for ANSI Common Lisp. How to Use This Book The best way to learn Lisp is to use it. It's also more fun to learn a language by writing programs in it. This book is designed to get you started as quickly as possible. After a brief Introduction, vii viii PREFACE • Chapter 2 explains, in 21 pages, everything you need to start writing Lisp programs. • Chapters 3-9 introduce the essential elements of Lisp programming. These chapters pay special attention to critical concepts like the role of pointers in Lisp, the use of recursion to solve problems, and the significance of first-class functions. For readers who want a thorough grounding in Lisp techniques, • Chapters 10-14 cover macros, CLOS, operations on list structure, opti mization, and advanced topics like packages and read-macros. • Chapters 15-17 sum up the lessons of the preceding chapters in three examples of real applications: a program for making logical inferences, an HTML generator, and an embedded language for object-oriented programming. The last part of the book consists of four appendices, which should be useful to all readers: • Appendices A-D include a guide to debugging, source code for 58 Common Lisp operators, a summary of the differences between ANSI Common Lisp and previous versions of the language,0 and a reference describing every operator in ANSI Common Lisp. The book concludes with a section of notes. The notes contain clarifications, references, additional code, and occasional heresies. Notes are indicated in the text by a small circle, like this.0 The Code Although it describes ANSI Common Lisp, this book has been designed so that you can use it with any version of Common Lisp. Examples that depend on newer features are usually accompanied by notes showing how they would be rendered in older implementations. All the code in this book is available online. You can find it, along with links to free software, historic papers, the Lisp FAQ, and a variety of other resources, at: http://www.eecs.harvard.edu/onlisp/ The code is also available by anonymous FTP from: ftp://ftp.eecs.harvard.edu:/pub/onlisp/ Questions and comments can be sent to pgOeecs. harvard. edu. PREFACE IX On Lisp Throughout this book I've tried to point out the unique qualities that make Lisp Lisp, and the new things that this language will let you do. Macros, for example: Lisp programmers can, and often do, write programs to write their programs for them. Lisp is the only major language in which this is a routinely used technique, because Lisp is the only major language to provide the abstractions that make it convenient. I would like to invite readers who are interested in learning more about macros and other advanced techniques to read the companion volume, On Lisp. Acknowledgements Of all the friends who have helped me during the writing of this book, I owe special thanks to Robert Morris. The whole book reflects his influence, and is very much the better for it. Several of the examples are derived from programs he originally wrote, including Henley (page 138) and the pattern-matcher on page 249. I was fortunate to have a first-rate team of technical reviewers: Skona Brittain, John Foderaro, Nick Levine, Peter Norvig, and Dave Touretzky. There is hardly a page of the book that did not benefit in some way from their suggestions. John Foderaro even rewrote some of the code in Section 5.7. Several other people consented to read all or part of the manuscript, including Ken Anderson, Tom Cheatham, Richard Fateman, Steve Hain, Barry Margolin, Waldo Pacheco, Wheeler Ruml, and Stuart Russell. Ken Anderson and Wheeler Ruml, in particular, made many useful comments. I'm grateful to Professor Cheatham, and Harvard generally, for providing the facilities used to write this book. Thanks also to the staff at Aiken Lab, including Tony Hartman, Dave Mazieres, Janusz Juda, Harry Bochner, and Joanne Klys. I'm glad to have had the chance to work with Alan Apt again. The people at Prentice Hall—Alan, Mona Pompili, Shirley McGuire, and Shirley Michaels—are really a pleasure to work with. The cover is again the work of the incomparable Gino Lee, of the Bow & Arrow Press, Cambridge. This book was typeset using L^TgX, a language written by Leslie Lamport atop Donald Knuth's Tj3C, with additional macros by L. A. Carr, Van Jacobson, and Guy Steele. The diagrams were done with Idraw, by John Vlissides and Scott Stanton. The whole was previewed with Ghostview, by Tim Theisen, which is built on Ghostscript, by L. Peter Deutsch. I owe thanks to many others, including Henry Baker, Kim Barrett, Ingrid Bassett, Trevor Blackwell, Paul Becker, Gary Bisbee, Frank Deutschmann, Frances Dickey, Rich and Scott Draves, Bill Dubuque, Dan Friedman, Jenny
Description: