Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 346 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
346
Dung lượng
2,52 MB
Nội dung
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 Knowle dge Bases Christos Papadimitriou The Theory of Database Concurrency Control Richard Snodgrass The Interface Description Language: Defini tion and Use Steven Tanimoto Elements of A rtificial 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 Scop e and Extent 165 Exercises 168 26 Sequences 171 Exercises 173 27 Lo cal 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.1 Organization 281 B.2 System-DependentOperations 282 B.3 ControlFunctions 283 B.3.1 VariableEnvironments 283 B.3.2 Assignment 284 B.3.3 Sequences 284 B.3.4 Exits 285 B.3.5 Conditionals 285 B.3.6 Iteration 287 B.3.7 MappingFunctions 287 B.4 Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . 288 B.5 Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 B.6 CLOS 290 B.7 Arrays 292 B.7.1 Constructors 292 B.7.2 Selectors 292 B.8 BooleanOperators 292 B.9 CharacterPredicates 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 [...]... be used to the functional style of pure Lisp and should be able to develop a good, balanced programming style A COMMON LISP Approach Rather than just translating the earlier version of this book into Common Lisp, I have incorporated a thorough Common Lisp approach Although Common Lisp is still obviously a dialect of Lisp, the quantitative additions of functions and features have made a qualitative difference... of Lisp programs and data structures We used to say that the Lisp language consisted of S-expressions, the major action of Lisp was the evaluation of S-expressions, and the read-eval-print loop consisted of reading an S-expression, evaluating it, and then printing the value as an S-expression A form, on the other hand, is a Common Lisp object that can be evaluated, and the major action of Common Lisp. .. purpose of this book is to teach the Common Lisp programming language The book is intended to be a self-paced study guide, requiring additional information from an instructor, manual, consultant, or friend only to fill in the details of the local operating system and a few implementationdependent features This book is a Common Lisp version of LISP: An Interactive Approach, published by Computer Science... creating the new version was the widespread adoption of Common 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 Common Lisp standard set out in Guy L Steele, Jr.’s COMMON LISP: The Language, Second Edition (Bedford, MA: Digital Press,... of a mature typing system, since Common Lisp has typed objects rather than typed expressions Previous Lisps had only two prominent data types: lists and atoms Numbers and strings existed but weren’t considered all that significant for Lisp programming Literal atoms were distinguished in several ways but didn’t form their own data type On the other hand, Common Lisp has an extensive set of types, each... To see the significance of Common Lisp s typing of objects, compare an untyped language such as Fortran with a strongly typed language such as Pascal with Common Lisp In Fortran, one may store a value of one type into a variable, and then pass that variable by reference to a procedure that operates on it as if it were another type In Pascal, the compiler would catch this as an error because the variable... understanding TO THE READER The purpose of this book is to help you learn the Common Lisp dialect of the programming language Lisp (LISt Processing language) by experimenting with it via an interactive computer terminal The recommended method is to read a chapter or two, sit down at a terminal and try the examples and exercises of those chapters, leave the terminal and go on to the next chapters, and... implementation of Common Lisp that you are using, or it might require first retrieving the Common Lisp system Once you have started your Common Lisp, you are ready to interact with it We say that you are about to interact with the top-level Lisp listener, or simply the top level of Lisp Most Common Lisp listeners will tell you they are waiting for input by printing a prompt This can be a greater-than symbol,... 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... for many disa cussions of abstract data types, which form the basis for the organization of A Appendix B; Ruth E Davis for providing me with her L TEX style file; the folks at Computer Science Press/ W H Freeman and Company, including Bill Gruener, Nola Hague, Tina Hastings, Diana Siemens, and Carol Loomis for their help and encouragement; and, above all, to my wife Caren, for her constant support and . this book into Common Lisp, I have incorporated a thorough Common Lisp approach. Although Common Lisp is still obviously a dialect of Lisp, the quantitative additions of functions and features. reading an S-expression, evaluat- ing it, and then printing the value as an S-expression. A form, on the other hand, is a Common Lisp object that can be evaluated, and the major action of Common Lisp. 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