IT training ANSI common lisp graham 1995 11 12

450 99 0
IT training ANSI common lisp graham 1995 11 12

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

ANSI Common Lisp UW§ PRENTICE HALL SERIES IN ARTIFICIAL INTELLIGENCE Stuart Russell and Peter Norvig, Editors GRAHAM MUGGLETON RUSSELL & NORVIG ANSI Common Lisp Logical Foundations of Machine Learning 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 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 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 Thefirsthalf 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 programmers 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 thefirsttime 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 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, optimization, 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 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 INDEX d e p o s i t - f i e l d function 348 describe function 389 d e s c r i b e - o b j e c t generic function 389 destructive operations 46, 55, 201, 222, 224 d e s t r u c t u r i n g - b i n d macro 318, 85, 103 d i g i t - c h a r function 355 d i g i t - c h a r - p function 355, 68 d i r e c t o r y function 373 directory-namestring function 371 disassemble function 389, 217 d i s j o i n 110 dispatch 185 diversity 100 DNA 1,414 macro 324,23,87 conceptually recursion 101 point of 89 do* macro 324, 88 do-all-symbols macro 344 do-external-symbols macro 344 do-symbols macro 344 documentation generic function 389, 100 documentation strings for functions 100 for slots 181 d o l i s t macro 324, 24, 88 dot notation 49 dotimes macro 324, 88 double-f l o a t - e p s i l o n constant 392 double-float-negative-epsilonconstant 392 dpb function 348 d r i b b l e function 389 Dylan 109 dynamic-extent declaration 225 dynamic allocation—see automatic memory management dynamic languages ecase macro 318, 244 echo-stream-input-stream function 374 echo-stream-output-stream function 374 ed function 389 eighth function 357, 40 e l t function 366, 63 Emacs 3, 17,407 embedded languages 269 encapsulation 191 via closures 108 419 via packages 136,191,238 encode-universal-time function 389, 404 endp function 357 enough-namestring function 371 ensure-directories-exist function 373 ensure-generic-function function 333 eq function 318,35,228 eql function 318, 16 equal function 318, 34,62 equality 34 numeric 146 structural 34 equalp function 318, 78 e r r o r function 339, 92, 244 errors catching quickly involving circular structure 209, 290 as conditions 244 correctable 245 division by zero 10, 411 involving dynamic scope 405 finding in backtrace 289 ignoring 246 infinite loops 289 guarding against 92 gross and avowed 403 involving keyword and optional parameters 293 misdeclarations 294 modifying constant structure 210 modifying shared structure 199 modifying traversed sequence 199 multiple evaluation 167 involving n i l 290,292 no longer possible obsolete inlined functions 217 omitting base case in recursion 43,290 overflow 150 read-macro intended to cause 131 recovering from 10, 245 caused by renaming 292 in user input 122, 246, 291 variable capture 166, 411 warnings 294 see also: name clashes error handler 92 • e r r o r - o u t p u t * variable 392 etypecase macro 319 eval function 314, 160, 413 evaluation avoiding 10 420 INDEX multiple 167, 169 order of evaluation rule eval-when special operator 314, 236 evenp function 349, 147 every function 319, 47 evolution of Lisp—see Lisp, evolution of of programs—see Lisp, evolution of programs in of programming styles exp function 349, 149 exponentiation 148 export function 345, 238 expression expt function 349, 148 extensibility exploration 6, 173, 230 factorials 89 Fahiman, Scott E 401 falsity 13 Fateman, Richard J 413 fbotmdp function 319,99 f c e i l i n g function 349 f d e f i n i t i o n function 319 • f e a t u r e s * variable 392 Felleisen, Matthias 402 f f l o o r function 349 Fibonacci function 116 f i f t h function 357,40 FIFO 200 f i l e - a u t h o r function 374 f i l e - e r r o r - p a t h n a m e generic function 374 f i l e - l e n g t h generic function 375 f i l e - n a m e s t r i n g function 371 f i l e - p o s i t i o n function 375 f i l e - s t r i n g - l e n g t h generic function 375 f i l e - w r i t e - d a t e function 374 f i l l function 367 f i l l - p o i n t e r function 362 fill-pointers 223 f i l t e r 105 find function 367, 65 f i n d - a l l - s y m b o l s function 345 f i n d - c l a s s function 333 f i n d - i f function 367, 65 f i n d - i f - n o t function 367 find-method generic function 333 find-package function 345, 236 f i n d - r e s t a r t function 339 find-symbol function 345 f i n i s h - o u t p u t function 375 f i r s t function 357, 40 fixnums 150,218,35 flames 407 Flanders f l e t special operator 319 f l o a t function 349, 144 f l o a t - d i g i t s function 349 f l o a t - p r e c i s ion function 349 f l o a t - r a d i x function 349 f l o a t - s i g n function 349 floating-point numbers—see numbers, floating-point f l o a t p function 349, 143 f l o o r function 349, 145 f makunbound function 319, 190 Foderaro, John K 173 for 170 force-output function 375 format function 379, 18, 123 building strings with 62 printing floats with 124 printing on a fresh line with 260 printing in lowercase with 260 printing times with 403 unpredictable rounding by 125 writing to a stream with 119 format directives 124 formatter macro 384 Fortune Cookie Institute 258 f o u r t h function 357, 40 f r e s h - l i n e function 375, 224 Friedman, Daniel P 402, 405 f round function 349 f t r u n c a t e function 349 f t ype declaration 315, 313 f u n c a l l function 319, 25 function special operator 319, 25 functions as arguments 25,110 anonymous 26 calling compiled 113 denning—see def un documentation strings of 100 generic—see generic functions interpreted 113 literal 26 local 101 vs macros 172,217 no main 15 as objects 25, 107 recursive—see recursion as return values 107 421 INDEX see also: closures, compilation functional programming 22 aids debugging 289 allows interactive testing 253 synergy with recursion 114 function-keywords generic function 333 function-lambda-expression function 319 functionp function 319 Gabriel, Richard P 28, 89, 284, 401 garbage 54 garbage collection 6, 54, 222 GC—see garbage collection gcd function 349 generic functions composed of methods 184 vs message-passing 192, 285 gensym function 342, 166 gensyms 136 used to avoid variable capture 166 used to encapsulate slots 408 •gensym-counter* variable 392 gentemp function 342 get function 342, 134 get-decoded-time function 389, 91 g e t - d i s p a t c h - m a c r o - c h a r a c t e r function 386 g e t - i n t e r n a l - r e a l - t i m e function 389 g e t - i n t e r n a l - r u n - t i m e function 389 get-macro-character function 386, 236 g e t - o u t p u t - s t r e a m - s t r i n g function 375 g e t - p r o p e r t i e s function 357 get-setf-expansionfunction 320,409 g e t - u n i v e r s a l - t i m e function 390 getf function 357 gethash function 370,76 global variables—see variables, special go special operator 320, 83 graphic-char-p function 355, 67 handlers 244 handler-bind macro 339 handler-case macro 340 hash tables creating 76 efficiency of 228 equality of keys in 78 expansion of 78 iteration on 78 vs lists 77 as sets 77 size of 78 h a s h - t a b l e - c o u n t function 370, 139 h a s h - t a b l e - p function 370 hash-table-rehash-size function 370 hash-table-rehash-threshold function 370 h a s h - t a b l e - s i z e function 370 h a s h - t a b l e - t e s t function 370 head 247 hoaxes 407 host-namestring function 371 HTML 257 hypertext 259 identity, testing 16, 34 i d e n t i t y function 320, 105 if special operator 320, 13 ignorable declaration 315, 313 ignore declaration 313, 294 i g n o r e - e r r o r s macro 340, 246 image plane 151 imagination 5, 222 imagpart function 349, 146 import function 345, 238 importing—see packages, importing into immediate data 35 in 170 in-package macro 345, 137 incf macro 349, 148 indentation 17 inference 247 information-hiding—see encapsulation inheritance 177, 272, 408 initargs 180 initforms 180 i n i t i a l i z e - i n s t a n c e generic function 334 i n l i n e declaration 315, 313 input 18, 121 as bytes 234 as characters 123 avoiding errors in 246 parsed 122 as strings 121 i n p u t - s t r e a m - p generic function 375 i n s p e c t function 390 instances vs classes 277 creating 177 vs structures 178, 179 integers—see numbers, integer 422 i n t e g e r - d e c o d e - f l o a t function 350 i n t e g e r - l e n g t h function 350 i n t e g e r p function 350, 143 intellectuals 188 interactive programming 3, 5, 6, 23, 95, 105, 215, 289 i n t e r a c t i v e - s t r e a m - p function 375 Interleaf i n t e r n function 345, 136 internal-time-units-per-secondconstant 392 Internet 259 interning—see symbols, interning of interpreters 215 i n t e r s e c t i o n function 357, 45 i n v a l i d - m e t h o d - e r r o r function 340 invoke-debugger function 340 i n v o k e - r e s t a r t function 340 i n v o k e - r e s t a r t - i n t e r a c t i v e l y function 340 I/O—see input, output, files, pathnames, streams i s q r t function 350 iteration 23,87 on hash tables 78 on lists 24 in pseudo-English 239 Janson, W J Keene, Sony a E 401,408 &key—see parameters, keyword keyword package 137 keywords 137 keyword arguments—see arguments, keyword keywordp function 342 Kiczales, Gregor 401, 408 Knuth, Donald E x, 213, 214 l a b e l s special operator 319, 101 Lambert's Law 154 lambda macro 314, 402 lambda expression 26 lambda symbol 402,26 lambda-list-keywords constant 392 lambda-parameters-limit constant 392 l a s t function 357, 39 lcm function 350 ldb function 350 l d b - t e s t function 350 l d i f f function 357 leap years 94 INDEX least-negative-double-float constant 393 least-negative-long-float constant 393 least-negative-normalizeddouble-f l o a t constant 393 least-negative-normalizedlong-f l o a t constant 393 least-negative-normalizeds h o r t - f l o a t constant 393 least-negative-normalizeds i n g l e - f l o a t constant 393 least-negative-short-float constant 393 least-negative-single-float constant 393 least-positive-double-float constant 393 least-positive-long-float constant 393 least-positive-normalizeddouble-f l o a t constant 393 least-positive-normalizedlong-f l o a t constant 393 least-positive-normalizeds h o r t - f l o a t constant 393 least-positive-normalizeds i n g l e - f l o a t constant 393 least-positive-short-float constant 393 least-positive-single-float constant 393 length function 367, 45 l e t special operator 320, 19, 83 l e t * special operator 320, 84 life 79 links 259 Lisp abstractness of dynamic character of evolution of 1, 104, 173 evolution of programs in 55, 79, 222, 277, 284, 402 expressive power of as an extension language as a language for writing Lisp 28 functional paradigm of 22 functions as objects in 25, 107 interactive nature of 7, 215 model of 161 name of 31 origin of present at every stage 93 programs expressed as lists 11, 160 INDEX programming style pure 19 role of recursion in 114 slowness of 284 speed of 413 as a specification language 4, 411 syntax of transcends OOP 2,285 two kinds of operators 228 users of 6, 114 see also: Common Lisp lisp-implementation-type function 390 l i s p - i m p l e m e n t a t i o n - v e r s i o n function 390 l i s t function 357, 11 lists access to elements of 39 accumulating 106 assoc(iation) 51 building 11 circular 208 compressing 36 concatenating 36 copying 36, 198 costs of 54 as a data type 11 dotted 49 doubly linked 204 empty 12 as expressions 9, 11 flat 33 as graphs 51 iteration on 24 mapping functions for 40 modifying 198 multiple ways to denote 50 nested 33 proper 49 property 134 as queues inefficiently 51 efficiently 200 quoted 210 as sets 43 splicing 164, 202 as stacks 47,200 structure shared between 195 avoiding 199 using 200 tails of 195,206 top-level structure of 197 as trees 40,197 underlying structure of 32 vs vectors 63 423 see also: backquote, conses, sequences l i s t * function 357 l i s t - a l l - p a c k a g e s function 345 l i s t - l e n g t h function 357 l i s t e n function 375 l i s t p function 357, 33 load function 388, 39, 137 load-logical-pathnamet r a n s l a t i o n s function 371 •load-pathname* variable 393 • l o a d - p r i n t * variable 393 load-time-value special operator 314 •load-truename* variable 393 • load-verbose* variable 393 local variables—see variables, lexical l o c a l l y special operator 314 log function 350, 149 logand function 350 logandcl function 350 logandc2 function 350 l o g b i t p function 350 logcount function 350 logeqv function 350 logical-pathname function 371 logical-pathname-translations function 371 l o g i o r function 350 lognand function 351 lognor function 351 lognot function 351 l o g o r c l function 351 logorc2 function 351 l o g t e s t function 351 logxor function 351 long-f l o a t - e p s i l o n constant 392 long-float-negative-epsilon constant 392 long-site-name function 390 loop macro 325,239,412 l o o p - f i n i s h macro 328 lower-case-p function 355 machine-instance function 390 machine-vers ion function 390 macros bugs in—see variables, capture of; evaluation, multiple built-in 14 as compilers 255 deciding which to write 173 defining 162 expansion of 162 formerly used to avoid calls 217 424 INDEX vs functions 172, 217 how not to use—see loop implementation of 163 role in evolution of Lisp 173 a n d s e t f 168,280 testing 162 see also: backquote macro characters—see read-macros macro-function function 315 macroexpand function 314 macroexpand-1 function 314, 162 •macroexpand-hook* variable 393 macrolet special operator 320 make-array function 363, 58 make-broadcast-stream function 375 make-concatenated-st ream function 375 make-condition function 340 make-dispatch-macro-character function 386,235 make-echo-stream function 375 make-hash-table function 370, 76 make-instance generic function 334, 179 make-instances-obsolete generic function 334 m a k e - l i s t function 357, 38 make-load-form generic function 334 make-load-form-saving-slots function 334 make-method macro 328 make-package function 345, 237 make-pathname function 372, 120 make-random-state function 351 make-sequence function 367 make-string function 364 m a k e - s t r i n g - i n p u t - s t r e a m function 375 m a k e - s t r i n g - o u t p u t - s t r e a m function 375 make-symbol function 342 make-synonym-stream function 376 make-two-way-stream function 376 makunbound function 343 manifest typing—see types, manifest map function 367, 224 map-int 105 map3 263 map-into function 367, 224 mapc function 357, 88 mapcan function 358, 202 mapcar function 358, 40 mapcon function 358 maphash function 370, 78 mapl function 358 maplist function 358, 40, 93 mappend 203 mapping functions 40, 199, 224 mask-field function 351 max function 351, 147 McCarthy, John 1,414 Meehan, James R 411 member function 358, 16,43 member-if function 358,44 member-if-not function 358 merge function 367, 264 merge-pathnames function 372 message-passing 192, 269, 285 methods 177 after- 188 around- 188 before- 188 applicable 185 auxiliary 187 combination of operator 189 standard 187 identified by specializations 186 primary 187 method-combination-error function 340 m e t h o d - q u a l i f i e r s generic function 334 Milton, John 139 function 351, 147 minusp function 351, 147 mismatch function 367 mod function 351, 145 models of programming •modules* variable 393 Moon, David A 401,405 most 105 most-negative-double-float constant 394 most-negative-f ixnum constant 394, 150 most-negative-long-float constant 394 most-negative-short-float constant 394 most-negative-single-float constant 394 most-positive-double-float constant 394 m o s t - p o s i t i v e - f ixnum constant 394, 150 most-positive-long-float constant 394 most-positive-short-float constant 394 425 INDEX most-positive-single-float constant 394 muffle-warning function 340 multimethods—see generic functions multiple return values 76, 89 m u l t i p l e - v a l u e - b i n d macro 320, 90 m u l t i p l e - v a l u e - c a l l special operator 320,91 m u l t i p l e - v a l u e - l i s t macro 320, 91 m u l t i p l e - v a l u e - p r o g l special operator 321 m u l t i p l e - v a l u e - s e t q macro 321 m u l t i p l e - v a l u e s - l i m i t constant 394 name-char function 355 name clashes between packages 238 between slot names 408 between variables due to macroexpansion—see errors, variable capture due to dynamic scope 405 namestring function 372 n b u t l a s t function 356 nconc function 358, 202 netnews 407 next-method-p function 334, 188 n i l constant 394, 12, 33, 292 n i l ! 162 n i n t e r s e c t i o n function 357, 222 n i n t h function 357,40 no-applicable-method generic function 334 no-next-method generic function 334 Norvig, Peter 410,412 not function 321, 13 not any function 321 not every function 321 not i n l i n e declaration 315, 313 nreconc function 359 nreverse function 369, 222 n s e t - d i f f erence function 359, 222 n s e t - e x c l u s i v e - o r function 359 n s t r i n g - c a p i t a l i z e function 365 nstring-downcase function 365 n s t r i n g - u p c a s e function 365 n s u b l i s function 359 nsubst function 360, 222 n s u b s t - i f function 360, 222 n s u b s t - i f - n o t function 360 n s u b s t i t u t e function 369 nsubst i t u t e - i f function 369 n s u b s t i t u t e - i f - n o t function 370 n t h function 358,39 n t h - v a l u e macro 321 n t h c d r function 358,39 ntimes 167 n u l l function 358, 13 numbers comparison of 146 complex 143 conversion to reals 144 extracting components of 146 floating-point 143 contagion 143 limits of 150,407 overflow 150 printing 124 types of 150 integer 11 no limit on size of 150 parsing 68 see also: bignums, fixnums random 146 ratio 143 conversion to integers 144 extracting components of 146 types of 143 converting between 144 numberp function 351, 20 numerator function 351, 146 nunion function 360, 222 object-oriented programming 176 analogy to hardware 176 benefits of 178 broad application of term 285 implementing 269 for reusable software 104 as a way to get run-time typing 410 and spaghetti code 408 transcended by Lisp 2, 285 two models of 192 see also: classes, CLOS, encapsulation, inheritance, instances, messagepassing, methods, multimethods, slots oddp function 352, 44, 147 oil paint 5,402 open function 376, 120 open-stream-p generic function 377 operator optimization destructive operations 222 efficient algorithms 214 fast operators 228 426 focus of 213 premature 214, 229 tail recursion—see recursion, tail see also: consing, avoiding optimize declaration 315, 313 feoptional—see parameters, optional or macro 321, 14 OS/360 otherwise symbol 316 output 18, 123 output-stream-p generic function 377 overflow—see numbers, floating-point packages based on names 239 default 136,236 defining 137 exporting from 137, 238 grossness of 411 importing into 238 nicknames of 137 purpose of 136,237 setting 137 used 137,239 •package* variable 394, 236 package-error-package generic function 345 package-name function 345, 236 package-nicknames function 345 package-shadowing-symbols function 345 p a c k a g e - u s e - l i s t function 346 p a c k a g e - u s e d - b y - l i s t function 346 packagep function 345 painting 5, 214, 402 p a i r l i s function 358 palindromes 46, 63 parentheses 8, 17 parameters 14 compilation—see compilation parameters congruent 186 efficiency of 228 keyword 103 optional 102 required 102 rest 102 specialized—see specialization p a r s e - i n t e g e r function 352, 68 parse-namestring function 372 pathname function 372 pathnames 120 pathname-device function 373 INDEX pathname-directory function 373 pathname-host function 372 pathname-mat ch-p function 373 pathname-name function 373 pathname-type function 373 pathname-vers ion function 373 pathnamep function 373 patterns for destructuring 85, 103 matching 249 peek-char function 377, 123 phase function 352 Perdue, Crispin 401 PGM 152 p i constant 394, 149 Pitman, Kent M 401 planning 5,229 plists—see property lists plusp function 352, 147 poetry 407 pointers avoiding 219 conses as pairs of 32 implicit 34 see also: lists pop macro 359, 47 p o s i t i o n function 367, 64 p o s i t i o n - i f function 367, 65 p o s i t i o n - i f - n o t function 368 p p r i n t function 384, 168 p p r i n t - d i s p a t c h function 384 pprint-exit-if-list-exhausted macro 384 p p r i n t - f i l l function 384 p p r i n t - i n d e n t function 384 p p r i n t - l i n e a r function 384 p p r i n t - l o g i c a l - b l o c k macro 384 p p r i n t - n e w l i n e function 385 p p r i n t - p o p macro 385 p p r i n t - t a b function 385 p p r i n t - t a b u l a r function 385 precedence 182 implementing 274 purpose of 183 prefix notation premature optimization 214 primary methods 187 p r i n l function 386, 123 p r i n l - t o - s t r i n g function 386 p r i n c function 385,123 p r i n c - t o - s t r i n g function 385 p r i n t function 385, 160 • p r i n t - a r r a y * variable 394, 59, 280 • p r i n t - b a s e * variable 394, 113 INDEX • p r i n t - c a s e * variable 394 • p r i n t - c i r c l e * variable 394, 208 • p r i n t - e s c a p e * variable 394 •print-gensym* variable 395 • p r i n t - l e n g t h * variable 395 • p r i n t - l e v e l * variable 395 • p r i n t - l i n e s * variable 395 • p r i n t - m i s e r - w i d t h * variable 395 p r i n t - n o t - r e a d a b l e - o b j ect generic function 385 p r i n t - o b j e c t generic function 385 *print-pprint-dispatch* variable 395 • p r i n t - p r e t t y * variable 395 • p r i n t - r a d i x * variable 395 • p r i n t - r e a d a b l y * variable 395 * p r i n t - r i g h t - m a r g i n * variable 395 p r i n t - u n r e a d a b l e - o b j e c t macro 386, 70 p r o b e - f i l e function 374 proclaim function 315, 215 profilers 214 prog macro 321 prog* macro 321 progl macro 321, 127, 274 prog2 macro 321 progn special operator 321, 24 progv special operator 321 prompt 7, 19 property lists 134 provide function 388 psetf macro 322 psetq macro 322 push macro 359,47 pushnew macro 359, 49 qualifiers 188 •query-io* variable 395 queues using lists as—see lists, as queues using vectors as 126 Quicksort 164 quote special operator 315, 10, 161 random function 352, 146 random-choice 170 •random-state* variable 395 random-state-p function 352 random text 138 rapid prototyping 3, 23, 401 rassoc function 359 r a s s o c - i f function 359 r a s s o c - i f - n o t function 359 427 ratios—see numbers, ratio r a t i o n a l function 352 r a t i o n a l i z e function 352 r a t ionalp function 352 ray-tracing 151 r c u r r y 110 read function 387, 18, 122 • r e a d - b a s e * variable 395 r e a d - b y t e function 377, 234 r e a d - c h a r function 377, 123 r e ad-char-no-hang function 377 •read-default-float-format* variable 396 r e a d - d e l i m i t e d - l i s t function 387, 236 *read-eval* variable 396, 406 r e a d - f r o m - s t r i n g function 387, 123 r e a d - l i n e function 377, 121 read-macros defining 235 dispatching 131, 235 predefined 399, 130 read-preserving-whitespace function 387 read-sequence function 377 *read-suppress* variable 396 • r e a d t a b l e * variable 396 r e a d t a b l e - c a s e function 387 r e a d t a b l e p function 387 r e a l p function 352 r e a l p a r t function 352, 146 Rees, Jonathan A 405, 411 recursion 16 efficiency of 116 and functional programming 114 can't be inlined 217 local 101 proper metaphor for 16 tail 116,215,289,409 using 114 verifying 42 reduce function 368, 66 extends two-argument functions 66 : key argument to 406 more efficient than apply 228 r e i n i t i a l i z e - i n s t a n c e generic function 334 rem function 352, 145 remf macro 359 remhash function 371 remove function 368, 22, 55, 66 remove-duplicates function 368, 66 remove-if function 368, 66 remove-if-not function 368 428 remove-method generic function 335 remprop function 343 rename-f i l e function 374 rename-package function 346 rendering 151 r e p l a c e function 369 Replicator 414 r e q u i r e function 388 ftrest—see parameters, rest r e s t function 359 r e s t a r t - b i n d macro 340 r e s t a r t - c a s e macro 341 r e s t a r t - n a m e function 341 r e t u r n macro 322, 82 return-from special operator 322, 81 reusable software 3, 104 revappend function 359 r e v e r s e function 369, 46 rewriting 262,402 rhyming dictionaries 224 ring buffers 126 risk 5, room function 390 r o t a t e f macro 322, 165 round function 352, 145 rounding to even digit 145 unpredictable, by format 125 row-major-aref function 363 row-major order 221 r p l a c a function 359 r p l a c d function 359 rules 247 run-length encoding 36 run-time typing 2,6,218,410 Russell, Stuart 412 s a f e t y compilation parameter 214 s b i t function 363 s c a l e - f l o a t function 352 schar function 364 Scheme 109,405,411 scope 112,405 search function 369 search binary 60 breadth-first 51 second function 357, 40 Sedgewick, Robert 402, 406 self-modifying programs 210 sequences 45, 63 access to elements of 63 copying segments of 46 INDEX finding the lengths of 45 finding elements of 64 removing duplicates from 66 replacing elements of 41 reversing 46 sorting 46, 164 see also: arrays, lists, vectors sequence functions 63 s e t function 343 sets hash tables as 77 lists as 43 s e t - d i f f e r e n c e function 359, 45 set-dispatch-macro-character function 387,235 s e t - e x c l u s i v e - o r function 359 set-macro-character function 387, 235 s e t - p p r i n t - d i s p a t c h function 386 set-syntax-from-char function 387 s e t f macro 322, 21 defining expansions of 100, 404 macros that expand into 168 macro call as first argument to 168 s e t q special operator 322 seventh function 357, 40 shadow function 346 shadowing-import function 346 Shalit, Andrew 405 s h a r e d - i n i t i a l i z e generic function 335 sharp-quote 25, 131 s h i f t f macro 322 s h o r t - f l o a t - e p s i l o n constant 392 s h o r t - f l o a t - n e g a t i v e - e p s i l o n constant 392 s h o r t - s i t e - n a m e function 390 shortest path 51 side-effects 19,22,201 s i g n a l function 341 signum function 353,146 s i m p l e - b i t - v e c t o r - p function 363 simple-condition-formatarguments generic function 341 simple-condition-format-control generic function 341 s i m p l e - s t r i n g - p function 364 simple-vector-p function 364 s i n function 353, 149 s i n g l e ? 105 s i n g l e - f l o a t - e p s i l o n constant 392 single-float-negative-epsilon constant 392 sinh function 353, 149 429 INDEX s i x t h function 357, 40 sleep function 390 slots accessors for 179 encapsulating 191 inheritance of 181, 408 initargs of 180 initforms of 180 properties of 179 shared 180 uninitialized 179 slot-boundp function 335 s l o t - e x i s t s - p generic function 335 slot-makunbound generic function 335 s l o t - m i s s i n g generic function 335 slot-unbound generic function 335 s l o t - v a l u e function 335 software-type function 390 software-version function 390 some function 323, 47 s o r t function 369, 46 sorting—see Quicksort, s o r t , s t a b l e - s o r t , binary search trees space compilation parameter 214 spaghetti code 408 s p e c i a l declaration 315, 313 specialization (array) 220 specialization (parameter) on classes 184 identifies method 186 on objects 186 restricted in message-passing 192 on types (apparent) 186 s p e c i a l - o p e r a t o r - p function 315 special operators 10, 161, 173 specifications 4, 229, 411 specific 182 Springer, George 405 speed compilation parameter 214 s q r t function 353, 149 square roots 149 s t a b l e - s o r t function 369, 402 stacks lists as 47 vectors as 223 standard method combination type 190, 187 standard-char-p function 355 • s t a n d a r d - i n p u t * variable 396, 119 standard-object class 182 •standard-output* variable 396, 119, 260 Starbuck, Orca 405 Steele, Guy Lewis Jr 89, 401, 405, 409 s t e p macro 390 s t ore-value function 342 Strassmann, Steve 405 stream-element-type generic function 377 s t r e a m - e r r o r - s t r e a m generic function 377 stream-external-format generic function 377 streamp function 378 s t r i n g function 364 strings access to elements of 62 concatenating 63 comparison of 62 creating 62 literal 11 sorting 62 vs symbols 138 as vectors of characters 61 see also: arrays, sequences, vectors streams binary 234 character 119 s t r i n g - c a p i t a l i z e function 365 string-downcase function 365, 140 s t r i n g - e q u a l function 365, 62 s t r i n g - g r e a t e r p function 365 s t r i n g - l e f t - t r i m function 365 s t r i n g - l e s s p function 365, 265 s t r i n g - n o t - e q u a l function 365 s t r i n g - n o t - g r e a t e r p function 365 s t r i n g - n o t - l e s s p function 365 s t r i n g - r i g h t - t r i m function 365 string substitution 125 s t r i n g - t r i m function 365 s t r i n g - u p c a s e function 365 s t r i n g / - function 366 s t r i n g < function 366, 265 string function 366 string>= function 366 s t r i n g p function 365 strong typing 218,410 structures 69 circularities in 209 default contents of 70 defining 69 display of 70 vs instances 178, 179 names of access functions for 70 as types 69 s u b l i s function 359, 252 430 subseq function 369, 46 subsetp function 360 subst function 360, 42 s u b s t - i f function 360 s u b s t - i f - n o t function 360 s u b s t i t u t e function 369, 41 s u b s t i t u t e - i f function 369 s u b s t i t u t e - i f - n o t function 370 subtypep function 315 superclasses 177, 181 Sussman, Gerald J 405 Sussman, Julie 405 svref function 364, 59, 220 sxhash function 371 symbols 11 conceptually atomic 138 exported—see packages, exporting from interning of 136,237 names of 133, 199 property lists of 134 qualified 137 vs strings 138 underlying structure of 135 uninterned—see gensyms uninterning 191 as variable names 15, 138 see also: packages symbol-function function 343, 99 symbolic computation 247 symbol-macrolet special operator 315 symbol-name function 343, 133 symbol-package function 343, 237 symbol-plist function 343, 135 symbol-value function 343, 138 symbolp function 343 synonym-stream-symbol function 378 T 411 t class 182 t constant 396, 13 tabloids 180 tags for catch 91 for go 83 in HTML 257 tagbody special operator 323, 81 tail call 216 t a i l p function 360, 195 tail recursion—see recursion t a n function 353, 149 function 353, 149 templates—see backquote tempera INDEX t e n t h function 357,40 * t e r m i n a l - i o * variable 396 t e r p r i function 378, 123 the special operator 315, 219, 410 t h i r d function 357, 13, 40 throw special operator 323, 91 time macro 390, 221 time machines 94 time-stamps 108 toplevel 7, 160, 215 Touretzky, David S 402 t r a c e macro 390, 288 • t r a c e - o u t p u t * variable 396 translate-logical-pathname function 373 translate-pathname function 373 trees binary 40 copying 41, 198 replacing elements of 42 see also: binary search trees t r e e - e q u a l function 360 truename function 374 t r u n c a t e function 353, 144 truth 13 two-way-stream-input-stream function 378 two-way-stream-output-stream function 378 type declaration 315, 313 types checking 244 declaring—see type declarations determining 27 hierarchy of 27,232 manifest 27,218 specifying 232 type declarations 218 for arrays 219 for expressions 219 for variables 218 macros to generate 410 optional nature of 218 for slots 181 violating 294 when useful 219 type-error-datum generic function 316 t y p e - e r r o r - e x p e c t e d - t y p e generic function 316 type-of function 316 typecase macro 323, 87, 107 typep function 316, 27 type specifiers—see types TBX 431 INDEX unbound-slot-instance function 335 unbound variable 291 underflow—see numbers, floating-point unexport function 346 unintern function 346, 191 union function 360, 45 universal time 404 unless macro 323, 85 unread-char function 378 untrace macro 391, 288 unwind-protect special operator 323, 92, 121 update-instance-for-differentc l a s s generic function 336 update-instance-for-redefined-class generic function 336 upgraded-array-element-type function 364 upgraded-complex-part-type function 353 upper-case-p function 355 use-package function 346, 239 use-value function 342 user-homedir-pathname function 391 u s e r - i n p u t 246 utilities 104,262 values function 323, 90 v a l u e s - l i s t function 323 variables capture of avoiding 166,411 intentional 172 free 107 special 20, 112, 138 implicit creation of 21 rebindingof 113,260 lexical 14,19,23,107,112,138 relation to symbols 138 shared 108 unbound 291 unused 294 vector function 364, 59 vectors access to elements of 59 creating 59 vs lists 63 literal 59 simple 59,220 see also: arrays, ring buffers vector-pop function 364, 223 vector-push function 364, 223 vector-push-extend function 364 vectorp function 364, 131 vi 17 warn function 342 warnings 294 Waters, Richard C 401 Web 257 Weinreb, Daniel L 401 when macro 323, 85 while 164 White, Jon L 401 wiId-pathname-p function 373 w i t h - a c c e s s o r s macro 335 w i t h - c o m p i l a t i o n - u n i t macro 388 w i t h - c o n d i t i o n - r e s t a r t s macro 342 with-gensyms 170 with-hash-table-iterator macro 371 w i t h - i n p u t - f r o m - s t r i n g macro 378 with-open-f i l e macro 378, 121 with-open-stream macro 378 w i t h - o u t p u t - t o - s t r i n g macro 379 w i t h - p a c k a g e - i t e r a t o r macro 346 w i t h - s i m p l e - r e s t a r t macro 342 w i t h - s l o t s macro 335 w i t h - s t a n d a r d - i o - s y n t a x macro 388 w r i t e function 386 w r i t e - b y t e function 379, 234 w r i t e - c h a r function 379 w r i t e - l i n e function 379 write-sequence function 379 w r i t e - s t r i n g function 379 w r i t e - t o - s t r i n g function 386 World Wide Web—see Web y - o r - n - p function 379 y e s - o r - n o - p function 379 zerop function 353, 39, 147 * variable 396, 54 * function 353, 148 ** variable 396, 54 *** variable 396, 54 + variable 396 + function 353, 147 ++ variable 396 +++ variable 396 - variable 354 - function 396, 147 / variable 396 / function 354, 148 432 //variable 396 ///variable 396 /= function 353, 146 1+ function 354, 148 1- function 354, 148 < function 353, 146 function 353, 146 >= function 353, 146 #! 235 #w# read-macro 399,208 # ' read-macro 399, 25, 131, 402 #( read-macro 399,59,131 #* read-macro 399 #+ read-macro 399 #- read-macro 399 # read-macro 399, 406 #: read-macro 399, 167 #< read-macro 399,70,131 #n= read-macrc> 399,208 #? 235 #A read-macro 399,59,131 #B read-macro 399 #C read-macro 399, 143 #0 read-macro 399 #P read-macro 399 #R read-macro 399 #S read-macro 399, 131 #X read-macro 399 # [ 235 # \ read-macro 399, 61 #] 235 #{ 235 # | read-macro 399, 61, 131 #} 235 ' read-macro '.399, 10, 130, 235 see also: quote ; read-macro '.399, 61 ' read-macro 399 see also: backquote # 235 , see backquote, comma within , see backquote, comma-at within : 137,238 : : 237 \ 134 133 INDEX ... Generic Functions 184 11. 7 Auxiliary Methods 187 11. 8 Method Combination 189 11. 9 Encapsulation 190 11. 10 Two Models 192 12 Structure 12. 1 12. 2 12. 3 12. 4 12. 5 12. 6 12. 7 12. 8 13 Speed 13.1 13.2... Example: Macro Utilities 169 On Lisp 173 11 CLOS 176 11. 1 Object-Oriented Programming 176 11. 2 Classes and Instances 179 11. 3 Slot Properties 179 11. 4 Superclasses 181 11. 5 Precedence 182 11. 6 Generic... 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

Ngày đăng: 05/11/2019, 15:08

Từ khóa liên quan

Mục lục

  • Contents

    • 1. Introduction

      • 1.1. New Tools

      • 1.2. New Techniques

      • 1.3. A New Approach

    • 2. Welcome to Lisp

      • 2.1. Form

      • 2.2. Evaluation

      • 2.3. Data

      • 2.4. List Operations

      • 2.5. Truth

      • 2.6. Functions

      • 2.7. Recursion

      • 2.8. Reading Lisp

      • 2.9. Input and Output

      • 2.10. Variables

      • 2.11. Assignment

      • 2.12. Functional Programming

      • 2.13. Iteration

      • 2.14. Functions as Objects

      • 2.15. Types

      • 2.16. Looking Forward

    • 3. Lists

      • 3.1. Conses

      • 3.2. Equality

      • 3.3. Why Lisp Has No Pointers

      • 3.4. Building Lists

      • 3.5. Example: Compression

      • 3.6. Access

      • 3.7. Mapping Functions

      • 3.8. Trees

      • 3.9. Understanding Recursion

      • 3.10. Sets

      • 3.11. Sequences

      • 3.12. Stacks

      • 3.13. Dotted Lists

      • 3.14. Assoc-lists

      • 3.15. Example: Shortest Path

      • 3.16. Garbage

    • 4. Specialized DataStructures

      • 4.1. Arrays

      • 4.2. Example: Binary Search

      • 4.3. Strings and Characters

      • 4.4. Sequences

      • 4.5. Example: Parsing Dates

      • 4.6. Structures

      • 4.7. Example: Binary Search Trees

      • 4.8. Hash Tables

    • 5. Control

      • 5.1. Blocks

      • 5.2. Context

      • 5.3. Conditionals

      • 5.4. Iteration

      • 5.5. Multiple Values

      • 5.6. Aborts

      • 5.7. Example: Date Arithmetic

    • 6. Functions

      • 6.1. Global Functions

      • 6.2. Local Functions

      • 6.3. Parameter Lists

      • 6.4. Example: Utilities

      • 6.5. Closures

      • 6.6. Example: FunctionBuilders

      • 6.7. Dynamic Scope

      • 6.8. Compilation

      • 6.9. Using Recursion

    • 7. Input and Output

      • 7.1. Streams

      • 7.2. Input

      • 7.3. Output

      • 7.4. Example: String Substitution

      • 7.5. Macro Characters

    • 8. Symbols

      • 8.1. Symbol Names

      • 8.2. Property Lists

      • 8.3. Symbols Are Big

      • 8.4. Creating Symbols

      • 8.5. Multiple Packages

      • 8.6. Keywords

      • 8.7. Symbols and Variables

      • 8.8. Example: Random Text

    • 9. Numbers

      • 9.1. Types

      • 9.2. Conversion andExtraction

      • 9.3. Comparison

      • 9.4. Arithmetic

      • 9.5. Exponentiation

      • 9.6. Trigonometric Functions

      • 9.7. Representation

      • 9.8. Example: Ray-Tracing

    • 10. Macros

      • 10.1. Eval

      • 10.2. Macros

      • 10.3. Backquote

      • 10.4. Example: Quicksort

      • 10.5. Macro Design

      • 10.6. Generalized Reference

      • 10.7. Example: MacroUtilities

      • 10.8. On Lisp

    • 11. CLOS

      • 11.1. Object-OrientedProgramming

      • 11.2. Classes and Instances

      • 11.3. Slot Properties

      • 11.4. Superclasses

      • 11.5. Precedence

      • 11.6. Generic Functions

      • 11.7. Auxiliary Methods

      • 11.8. Method Combination

      • 11.9. Encapsulation

      • 11.10. Two Models

    • 12. Structure

      • 12.1. Shared Structure

      • 12.2. Modification

      • 12.3. Example: Queues

      • 12.4. Destructive Functions

      • 12.5. Example: Binary SearchTrees

      • 12.6. Example: Doubly-LinkedLists

      • 12.7. Circular Structure

      • 12.8. Constant Structure

    • 13. Speed

      • 13.1. The Bottleneck Rule

      • 13.2. Compilation

      • 13.3. Type Declarations

      • 13.4. Garbage Avoidance

      • 13.5. Example: Pools

      • 13.6. Fast Operators

      • 13.7. Two-Phase Development

    • 14. Advanced Topics

      • 14.1. Type Specifiers

      • 14.2. Binary Streams

      • 14.3. Read-Macros

      • 14.4. Packages

      • 14.5. The Loop Facility

      • 14.6. Conditions

    • 15. Example: Inference

      • 15.1. The Aim

      • 15.2. Matching

      • 15.3. Answering Queries

      • 15.4. Analysis

    • 16. Example: Generating HTML

      • 16.1. HTML

      • 16.2. HTML Utilities

      • 16.3. An Iteration Utility

      • 16.4. Generating Pages

    • 17. Example: Objects

      • 17.1. Inheritance

      • 17.2. Multiple Inheritance

      • 17.3. Defining Objects

      • 17.4. Functional Syntax

      • 17.5. Defining Methods

      • 17.6. Instances

      • 17.7. New Implementation

      • 17.8. Analysis

    • A. Debugging

    • B. Lisp in Lisp

    • C. Changes to Common Lisp

    • D. Language Reference

    • Notes

    • Index

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan