COMMON LISP an Interactive Approach PRINCIPLES of COMPUTER SCIENCE SERIES Series Editors Alfred V
Total Page:16
File Type:pdf, Size:1020Kb
COMMON LISP An Interactive Approach PRINCIPLES OF COMPUTER SCIENCE SERIES Series Editors Alfred V. Aho, Bellcore, Morristown, New Jersey Jeffrey D. Ullman, Stanford University, Stanford, California Egon B¨orger, Editor Trends in Theoretical Computer Science Ruth E. Davis Truth, Deduction, and Computation: Logic and Semantics for Computer Science Nicholas J. DeLillo A First Course in Computer Science with ADA A. K. Dewdney The Turing Omnibus: 61 Excursions in Computer Science Vladimir Drobot Formal Languages and Automata Theory Eitan M. Gurari An Introduction to the Theory of Computation Martti M¨antyl¨a An Introduction to Solid Modeling Bertrand Meyer Software Engineering: Principles and Practices Shamim Naqvi and Shalom Tsur A Logical Language for Data and Knowledge Bases Christos Papadimitriou The Theory of Database Concurrency Control Richard Snodgrass The Interface Description Language: Definition and Use Steven Tanimoto Elements of Artificial Intelligence Using COMMON LISP Jeffrey D. Ullman Computational Aspects of VLSI Jeffrey D. Ullman Principles of Database and Knowledge-Base Systems, Volume I: Classical Database Systems Jeffrey D. Ullman Principles of Database and Knowledge-Base Systems, Volume II: The New Tech- nologies Jeffrey D. Ullman Theory of Relational Databases COMMON LISP An Interactive Approach STUART C. SHAPIRO State University of New York at Buffalo COMPUTER SCIENCE PRESS AN IMPRINT OF W. H. FREEMAN AND COMPANY • NEW YORK Library of Congress Cataloging-in-Publication Data Shapiro, Stuart Charles Common LISP: an interactive approach / by Stuart C. Shapiro. p. cm. Includes index. ISBN 0-7167-8218-9 1. LISP (Computer program) I. Title. II. Title: LISP. QA76.73.L23S53 1991 005. 13’3—dc20 91-12377 CIP Copyright c 1992 by Stuart C. Shapiro No part of this book may be reproduced by any mechanical, photographic, or electronic process, or in the form of a phonographic recording, nor may it be stored in a retrieval system, transmitted, or otherwise copied for public or private use, without written permission from the publisher. Printed in the United States of America Computer Science Press An imprint of W. H. Freeman and Company The book publishing arm of Scientific American 41 Madison Avenue, New York, NY 10010 20 Beaumont Street, Oxford OX1 2NQ, England 1234567890RRD9987654321 To Caren Contents Preface xiii To the Reader xxi ITHEBASICS 1 1 Getting Started 3 Exercises................................ 5 2Numbers 7 Exercises................................ 9 3Lists 11 Exercises................................ 14 4 Arithmetic 15 Exercises................................ 18 5StringsandCharacters 21 Exercises................................ 23 6Symbols 27 Exercises................................ 32 vii viii 7Packages 35 Exercises................................ 42 8 Basic List Processing 45 Exercises................................ 49 9 The Special Form quote 51 Exercises................................ 51 II PROGRAMMING IN PURE LISP 53 10 Defining Your Own Functions 55 Exercises................................ 58 11 Defining Functions in Packages 61 Exercises................................ 65 12 Saving for Another Day 67 Exercises................................ 70 13 Predicate Functions 73 Exercises................................ 75 14 Conditional Expressions 77 Exercises................................ 79 15 Recursion 81 Exercises................................ 85 16 Recursion on Lists, Part 1—Analysis 89 Exercises................................ 93 17 Recursion on Lists, Part 2—Synthesis 97 Exercises................................ 103 18 Recursion on Trees 111 Exercises................................ 120 19 The Evaluator 127 Exercises................................ 130 20 Functions with Arbitrary Numbers of Arguments 135 Exercises................................ 137 ix 21 Mapping Functions 139 Exercises................................ 142 22 The Applicator 145 Exercises................................ 148 23 Macros 151 Exercises................................ 154 III PROGRAMMING IN IMPERATIVE LISP 157 24 Assignment 159 Exercises................................ 162 25 Scope and Extent 165 Exercises................................ 168 26 Sequences 171 Exercises................................ 173 27 Local Variables 177 Exercises................................ 179 28 Iteration 181 Exercises................................ 190 29 Input/Output 193 Exercises................................ 198 30 Destructive List Manipulation 203 Exercises................................ 209 31 Property Lists 213 Exercises................................ 215 32 Hash Tables 219 Exercises................................ 224 IV OBJECT-ORIENTED PROGRAMMING 227 33 Methods 229 Exercises................................ 234 x 34 Classes 237 Exercises................................ 256 V APPENDICES 259 A Solutions to Selected Exercises 261 B COMMON LISP Reference Manual 281 B.1Organization........................... 281 B.2System-DependentOperations................. 282 B.3ControlFunctions........................ 283 B.3.1VariableEnvironments.................. 283 B.3.2Assignment........................ 284 B.3.3Sequences......................... 284 B.3.4Exits............................ 285 B.3.5Conditionals....................... 285 B.3.6Iteration.......................... 287 B.3.7MappingFunctions.................... 287 B.4 Utility Functions . 288 B.5 Input/Output . 288 B.6CLOS............................... 290 B.7Arrays............................... 292 B.7.1Constructors....................... 292 B.7.2Selectors.......................... 292 B.8BooleanOperators........................ 292 B.9CharacterPredicates....................... 293 B.10FileOperators.......................... 293 B.11Functions............................. 293 B.11.1Constructors....................... 293 B.11.2Operators......................... 293 B.12HashTables............................ 294 B.12.1Constructors....................... 294 B.12.2Selectors.......................... 294 B.12.3Attributes......................... 294 B.12.4Operators......................... 294 B.13ListsandConses......................... 294 B.13.1Constructors....................... 294 B.13.2Selectors.......................... 295 B.13.3Predicates......................... 297 B.13.4Operators......................... 298 B.14Numbers.............................. 298 B.14.1Constructors....................... 298 B.14.2Predicates......................... 300 xi B.14.3Operators......................... 300 B.15Objects.............................. 301 B.15.1Constructors....................... 301 B.15.2Predicates......................... 302 B.15.3Attributes......................... 303 B.15.4Operators......................... 303 B.16Packages.............................. 304 B.16.1Constructors....................... 304 B.16.2Selectors.......................... 304 B.16.3Operators......................... 304 B.17Sequences............................. 306 B.17.1Selectors.......................... 306 B.17.2Attributes......................... 306 B.17.3Operators......................... 306 B.18Strings............................... 307 B.18.1Selectors.......................... 307 B.18.2Predicates......................... 307 B.19Symbols.............................. 307 B.19.1Constructors....................... 307 B.19.2Selectors.......................... 309 B.19.3Predicates......................... 310 B.19.4Attributes......................... 310 B.19.5Operators......................... 310 Index 313 PREFACE The purpose of this book is to teach the Common Lisp programming lan- guage. The book is intended to be a self-paced study guide, requiring ad- ditional information from an instructor, manual, consultant, or friend only to fill in the details of the local operating system and a few implementation- dependent features. This book is a Common Lisp version of LISP: An Inter- active Approach, published by Computer Science Press in 1986. The major motivation for creating the new version was the widespread adoption of Com- mon Lisp as the standard Lisp dialect. In the earlier edition, I presented Lisp in a dialect-independent way and discussed the different approaches of the major dialects. In this edition, however, I am strictly following the Com- mon Lisp standard set out in Guy L. Steele, Jr.’s COMMON LISP: The Language, Second Edition (Bedford, MA: Digital Press, 1990). (Steele’s book is often referred to as CLtL-2, and I will do so hereafter.) The Lisp version of this book has been used as the text of the Lisp portion of data structures, programming languages, and artificial intelligence courses and as a self-study guide for students, faculty members, and others learning Lisp independently. Draft versions of this book have also been used in Common Lisp courses, artificial intelligence courses, and for self-study. xiii xiv PREFACE Teaching LISP Lisp is the language of choice for work in artificial intelligence and in symbolic algebra. It is also important in the study of programming languages, because, since its inception over thirty years ago, it has had full recursion, the con- ditional expression, the equivalence of program and data structure, its own evaluator available to the programmer, and extensibility—the syntactic in- distinguishability of programmer-defined functions and “built-in” operators. It is also the paradigm of “functional,” or “applicative,” programming. Be- cause of the varied interests in Lisp, I have tried to present it in a general and neutral setting, rather