Book

Click here to load reader

  • date post

    15-Aug-2014
  • Category

    Documents

  • view

    904
  • download

    2

Embed Size (px)

Transcript of Book

COMMON LISP: A Gentle Introduction to Symbolic Computation

COMMON LISP: A Gentle Introduction to Symbolic Computation

David S. TouretzkyCarnegie Mellon University

The Benjamin/Cummings Publishing Company,Inc.Redwood City, California Fort Collins, Colorado Menlo Park, California Reading, Massachusetts New York Don Mill, Ontario Workingham, U.K. Amsterdam Bonn Sydney Singapore Tokyo Madrid San Juan

Sponsoring Editor: Alan Apt Developmental Editor: Mark McCormick Production Coordinator: John Walker Copy Editor: Steven Sorenson Text and Cover Designer: Michael Rogondino Cover image selected by David S. Touretzky Cover: La Grande Vitesse, sculpture by Alexander Calder

Copyright (c) 1990 by Symbolic Technology, Ltd. Published by The Benjamin/Cummings Publishing Company, Inc. This document may be redistributed in hardcopy form only, and only for educational purposes at no charge to the recipient. Redistribution in electronic form, such as on a web page or CD-ROM disk, is prohibited. All other rights are reserved. Any other use of this material is prohibited without the written permission of the copyright holder. The programs presented in this book have been included for their instructional value. They have been tested with care but are not guaranteed for any particular purpose. The publisher does not offer any warranties or representations, nor does it accept any liabilities with respect to the programs. Library of Congress Cataloging-in-Publication Data Touretzky, David S. Common LISP : a gentle introduction to symbolic computation / David S. Touretzky p. cm. Includes index. ISBN 0-8053-0492-4 1. COMMON LISP (Computer program language) I. Title. QA76.73.C28T68 1989 005.133dc20 89-15180 CIP ISBN 0-8053-0492-4

ABCDEFGHIJK - DO - 8932109

The Benjamin/Cummings Publishing Company, Inc.390 Bridge Parkway Redwood City, California 94065

To Phil and Anne

Preface

This book is about learning to program in Lisp. Although widely known as the principal language of artificial intelligence researchone of the most advanced areas of computer scienceLisp is an excellent language for beginners. It is increasingly the language of choice in introductory programming courses due to its friendly, interactive environment, rich data structures, and powerful software tools that even a novice can master in short order. When I wrote the book I had three types of reader in mind. I would like to address each in turn. Students taking their first programming course. The student could be from any discipline, from computer science to the humanities. For you, let me stress the word gentle in the title. I assume no prior mathematical background beyond arithmetic. Even if you dont like math, you may find you enjoy computer programming. Ive avoided technical jargon, and there are lots of examples. Also you will find plenty of exercises interspersed with the text, and the answers to all of them are included in Appendix C. Psychologists, linguists, computer scientists, and other persons interested in Artificial Intelligence. As you begin your inquiry into AI, you will see that almost all research in this field is carried out in Lisp. Most Lisp texts are written exclusively for computer science majors, but I have gone to great effort to make this book accessible to everyone. It can be your doorway to the technical literature of AI, as well as a quick introduction to its central tool. Computer hobbyists. Prior to about 1984, the Lisps available on personal computers werent very good due to the small memories of the early machines. Todays personal computers often come with several megabytes of RAM and a hard disk as standardvii

viii

Common Lisp: A Gentle Introduction to Symbolic Computation

equipment. They run full implementations of the Common Lisp standard, and provide the same high-quality tools as the Lisps in university and industrial research labs. The Lisp Toolkit sections of this book will introduce you to the advanced features of the Common Lisp programming environment that have made the language such a productive tool for rapid prototyping and AI programming. This current volume of the gentle introduction uses Common Lisp throughout. Lisp has been changing continuously since its invention 30 years ago. In the past, not only were the Lisp dialects on different machines incompatible, but programs written in one dialect would often no longer run in that same dialect a few years later, because the language had evolved out from under them. Rapid, unconstrained evolution was beneficial in the early days, but demand for a standard eventually grew, so Common Lisp was created. At present, Common Lisp is the de facto standard supported by all major computer manufacturers. It is currently undergoing refinement into an official standard. But Lisp will continue to evolve nonetheless, and the standard will be updated periodically to reflect new contributions people have made to the language. Perhaps one of those contributors will be you.DAVID S. TOURETZKY PITTSBURGH, PENNSYLVANIA

Note to Instructors

Much has been learned in the last few years about how to teach Lisp effectively to beginners: where they stumble and what we can do about it. In addition, the switch to Common Lisp has necessitated changes in the way certain topics are taught, especially variables, scoping, and assignment. This version of the gentle introduction has been completely revised for Common Lisp, and includes several new teaching tools that I believe you will find invaluable in the classroom. Let me share with you some of the thinking behind this books novel approach to Lisp.

GRAPHICAL NOTATIONThe first two chapters use a graphical box-and-arrow notation for describing primitive functions and function composition. This notation allows students to get comfortable with the basic idea of computation and the three fundamental data structuresnumbers, symbols, and listsbefore grappling with side issues such as the syntax of a function call or when to use quotes. Although sophisticated Lispers profit from the realization that programs are data, to the beginner this is a major source of confusion. The box-and-arrow notation makes programs and data visually distinct, and thereby eliminates most syntax errors. Another advantage of this notation is its lack of explicit variables; the inputs to a function are simply arrows that enter the function definition from outside. Since there is no computer implementation of function box notation, the first two chapters are designed to be covered rapidly using just pencil and paper. This also shelters the student temporarily from another source of frustrationlearning the mechanics of using an actual machine, editing expressions, and coping with the debugger. Readers who are familiar with other programming languages can flip through Chapter 1 in a minute or so, read the summary at the end, and then skim Chapter 2 to pick up the basic list manipulation primitives.ix

x

Common Lisp: A Gentle Introduction to Symbolic Computation

In Chapter 3 the student is introduced to standard EVAL notation; the concepts of quoting and named variables follow fairly naturally. Now he or she is ready to discard paper and pencil for a real computer (and is probably eager to do so), whereas at the start of the course this might have been viewed with trepidation.

OTHER FEATURESThree other unique features of the book first appear in Chapter 3: evaltrace notation, Lisp Toolkit sections, and a comprehensive graphical representation for Lisp data structures, including function objects and the internal structure of symbols. Evaltrace notation shows step-by-step how Lisp expressions are evaluated, how functions are applied to arguments, and how variables are created and bound. The different roles of EVAL and APPLY, the scoping of variables, and the nesting of lexical contours can all be explained graphically using this notation. It makes the process of evaluation transparent to the student by describing it in a visual language which he or she can remember and use. The Lisp Toolkit sections introduce the various programming aids that Common Lisp provides, such as DESCRIBE, INSPECT, TRACE, STEP, and the debugger. There are also two tools unique to this book; their source code appears in Appendices A and B, and is available on diskette from the publisher. The first tool, SDRAW, draws cons cell diagrams. It is part of a read-eval-draw loop that has proven invaluable for teaching beginners to reason about cons cell structures, particularly the differences among CONS, LIST, and APPEND. The second tool, DTRACE, is a tracing package that generates more detailed output than most implementations of TRACE, and is therefore more useful for teaching beginners. Finally, the graphical representation of Lisp data structuresparticularly the internal structure of symbols with their name, function, value, plist, and package cellshelps students understand the true nature of Lisp interpreters and highlights the distinctions between symbols, functions, variables, and print names.

ORGANIZATION OF LATER CHAPTERSApplicative operators are introduced in Chapter 7, where the student also learns about lexical closures. In Chapter 8, the dragon stories that were a popular feature of the previous version have been retained, but they are now backed up with a new devicerecursion templatesthat helps beginners analyze recursive functions to extract the essence of the recursive style. Since

Note to Instructors

xi

some instructors prefer to teach recursion before applicatives, these two chapters have been written so that they may be covered in either order. The book promotes a clean, side-effect-free style of programming for the first eight chapters. Chapter 9 discusses i/o. Chapter 10 provides a unified picture of assignment that includes ordinary variables, generalized variables, and destructive sequence operations. Chapter 11 covers iteration, and shows how DO and DO* can be used to construct substantial iterative expressions with no explicit assignments. Chapter 12 introduces structures, and Chapter 13 covers arrays, hash tables, and property lists. The final chapter, Chapter 14, is devoted to macros and compilation. It also explains the difference between lexical and dynamic scoping. Evaltrace diagrams clarify the semantics of macros and special variables.

EMPHASIS ON SIMPLICITYBecause Common Lisp is such a complex language, there are a few places where I have chosen to simplify things to better meet the needs of beginners. For example, the 1+ and 1- functions are banished from this book because their names are very confusing. Also, the book relies almost exclusively on EQUAL because this is the most useful equality predicate. EQ, EQL, EQUALP, and = are mentioned in advanced topics sections, but not used very much. In a few places I have chosen to write a function slightly less concisely rather than introduce one of the more obscure primitives like PUSHNEW. And I make no attempt to cover the most advanced features, such as multiple values or the package system. Some people prefer to teach Scheme in introductory courses because it is so much smaller than Common Lisp. But one can easily teach the subset of Common Lisp that is equivalent to Scheme, so language size isnt really an issue for beginners. A more compelling argument is that there is a certain style of applicative programming, making heavy use of lexical closures, that can be expressed more elegantly in Scheme syntax. But there are also areas where Common Lisp is superior to Scheme, such as its support for userdefined macros, its elegant unification of lists and vectors into a sequence datatype, and its use of keyword arguments to greatly extend the utility of the sequence functions. The combination of tremendous power, extensive manufacturer support, and a built-in object-oriented programming facility make Common Lisp the only industrial strength Lisp. Although this book does emphasize a side-effect-free, applicative approach to programming with which Scheme afficionados will feel quite at home, it does so in purely Common Lisp style.

xii

Common Lisp: A Gentle Introduction to Symbolic Computation

This book has been carefully designed to meet the needs of beginning programmers and non-computer science students, but the optional advanced topics sections at the end of each chapter provide enough enrichment material to hold the interest of junior and senior computer science majors. For advanced undergraduates, Guy L. Steele Jr.s Common Lisp: The Language (published by Digital Press) would be a useful companion to the introduction provided here. For beginners, Common Lisp: The Reference, by Franz, Inc. (published by Addison-Wesley) is a more suitable reference work.

Acknowledgements

This book began in 1981 as a set of notes for a programming course for humanities students at Carnegie Mellon University. I am greatly indebted to Phil Miller for the administrative support that made the course possible. John McDermott and Scott Fahlman also helped with administrative matters. My second major debt is to Anne Rogers, who took it upon herself to edit early drafts of the manuscript. Anne was an irrepressible source of encouragement; her enthusiasm kept the book alive through difficult times. Loretta Ferro, Maria Wadlow, and Sandy Esch kindly served as test subjects in my first pedagogical experiments. I also thank my students in the first actual Lisp course for the time and energy they put into it. Gail Kaiser, Mark Boggs, Aaron Wohl, and Lynn Baumeister all taught the new Lisp course using my notes. Their feedback helped improve succeeding drafts. Richard Pattis, author of another fine programming text, was an able publicity agent and ultimately helped me find my first publisher, Harper & Row. Abby Gelles also helped publicize the book. At Harper & Row, John Willig taught me about academic publishing and Mexican food, and remains a good friend. Throughout the preparation of the previous version I was most fortunate to be supported by a graduate fellowship from the Fannie and John Hertz Foundation. In 1987, Harper & Row left the computer science publishing business. John Wiley & Sons took over distribution of the previous version while I found a publisher for this volume. The book found a new home at Benjamin/ Cummings thanks to the patience and diligence of executive editor Alan Apt. I thank Mark Fox, at the time acting president of Carnegie Group, Inc., for permission to include some software in the current volume that I originally developed for his company. I also thank the reviewers who contributed the most valuable advice on improving the current volume: Skona Brittain, Mikexiii

14

Common Lisp: A Gentle Introduction to Symbolic Computation

Clancy, Rich Pattis, and Douglas Dankel. Other useful comments were received from Rick Wilson, Sharon Salveter, Terrance Boult,...