ebook img

Programming and Meta-Programming in Scheme PDF

351 Pages·1998·19.571 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming and Meta-Programming in Scheme

UNDERGRADUATE TEXTS IN COMPUTER SCIENCE Editors David Gries Fred B. Schneider Springer Science+Business Media, LLC UNDERGRADUATE TEXTS IN COMPUTER SCIENCE Beidler, Data Structures and Algorithms Brooks, Problem Solving with Fortran 90 Grillmeyer, Exploring Computer Science with Scheme Jalote, An Integrated Approach to Software Engineering, Second Edition Kizza, Ethical and Social Issues in the Information Age Kozen, Automata and Computability Merritt and Stix, Migrating from Pascal to C++ Pearce, Programming and Meta-Programming in Scheme Zeigler, Objects and Systems Jon Pearce Programming and Meta-Programming in Scheme , Springer JonPearce Department of Mathematics and Computer Science San Jose University San Jose, CA 95192-0103 USA Series Editors: David Gries Fred B. Schneider Department of Computer Science Cornell University Upson HalI Ithaca, NY 14853-7501 USA Library of Congress Cataloging-in-Publication Data Pearce, Jon. Programming and meta-programming in Scheme / Jon Pearce. p. cm. - (Undergraduate texts in computer science) Includes bibliographica1 references and index. ISBN 978-1-4612-7243-4 ISBN 978-1-4612-1682-7 (eBook) DOI 10.1007/978-1-4612-1682-7 1. Scheme (Computer program language) 1. Title. II. Series QA76.73.s34P4 1997 005.13'~c21 97-28476 Prînted on acid-free paper. © 1998 Springer Science+Business Media New York Originally published by Springer-VerlagNew York,Inc. in 1998 Softcover reprint ofthe hardcover Ist edition 1998 AII rights reserved. This work may not be translated or copied in whole or in part without the written permis sion of the publisher, Springer Science+Business Media, LLC, except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation, computer software, or by similar or dissimiJar methodology now known or hereafter developed is forbidden. The use of general descriptive names, trade names, trademarks, etc., in this publication, even if the former are not especialIy identified, is not to be taken as a sign that such names, as understood by the Trade Marks and Merchandise Marks Act, may accordingly be used freely by anyone. Production managed by Steven Pisano; manufacturing supervised by Johanna Tschebull. Photocomposed pages prepared from the author's Microsoft Word files. 987654321 ISBN 978-1-4612-7243-4 To my parents Preface The current domination of computing by C and C++ is so complete that publishing a book on Scheme almost seems subversive. My heresy goes beyond language. Infonned by the anticorporate intellectual values of my computer gurus, I have come to possess the unpopular notion that ideas are more important than technology, that principles like ab straction and modularity take us further than gigabytes and megahertz. This is really a book about those ideas disguised as a book about Scheme. Scheme was selected partly for historical reasons, but mostly because it allows us to encounter ideas without first mastering memory management, compilers, 110, and advanced idioms. I want to con gratulate my co-conspirators at Springer for their courage in joining me. For the last five years I have successfully used drafts of this book as a text in a junior level declarative programming course taught at San Jose State University. The course is modeled after Scheme-based freshman-level courses taught at other universities, hence prerequisites are minimal. Motivated students with basic computer and mathematical lit eracy will be able to read this book. By confining lectures to the core sections of each chapter and a few critical appendi ces, leaving the other appendices and problem sections as homework assignments, I typi cally manage to cover all but the last chapter in a semester. Occasionally students read and solve problems from the last chapter as special projects. Students who finish the course know Scheme. They have been introduced to important principles, concepts, paradigms, and techniques, and they have a functional perspective they confidently carry into subsequent courses. Jon Pearce Contents Preface vii Introduction 1 1.1 LISP.............................................................................................................. 1 1.1.1. liSP Dialects ................................................................................. 2 1.2 Scheme ............................ ........ ......................... ........... .......................... ... .... 2 1.2.1. Scheme on the Web........................................................................ 3 1.3. Structure of the Text.................................................................................... 3 /.3.1. Themes ........................................................................................... 4 1. Expressions and Values 5 1.1. Values ..... ,.................................................................................................... 5 1.1.1. Numbers ...................................., .................................................... 6 1.1.2. Characters...................................................................................... 6 1.1.3. Booleans......................................................................................... 7 1./ .4. Symbols .......................................................................................... 7 1.1.5. Procedures ..................................................................................... 8 1.1.6. Strings ............................................................................................ 9 1.1.7. Lists ................................................................................................ 10 1.I.B. Vectors ........................................................................................... 10 1.1.9. Pairs............................................................................................... 10 1.1.10. Other Value Domains ................................................................... 11 1.2. Expressions ............................................. ... ... ....... ............. ....... .................... 11 1.2.1. Literals ........................................................................................... 12 1.2.2. Symbols and the Global Environment............................................ 12 1.2.3. Applications ................................................................................... 13 1.2.4. Structures ....................................................................................... 16 1.2.5. Literals Revisited............................................................................ 16 1.3. The Scheme Interpreter................................................................................ 17 1.3.1. The Expression Evaluator .............................................................. 17 1.3.2. The Control Loop ........................................................................... 18 1.4. Defmitions ................................................................................................... 18 Appendices. ...................... ...... ................ ... ............ .... ......... ..... .... .................. ..... 20 Appendix 1.1. Defining Domains ............................................................. 20 x Contents Appendix 1.2. Sessions ............................................................................. 22 Appendix 1.3. Numbers ............................................................................ 24 Problems ..................•..................................................................•....................... 26 2. Procedures 31 2.1. Derming and Applying Procedures ............................................................. 31 2.1.1. The Environmental Influence ......................................................... 32 2.1.2. The Modularity Principle and Top-Down Design.......................... 34 2.2. Building Procedures Using Application ...................................................... 36 2.2.1. Example: Coercions ....................................................................... 36 2.2.2. Example: Palindromes ................................................................... 39 2.3. The Abstraction Principle ..........•...•.•.............••••...........•.....•...•.•.................. 40 2.3.1. Constructors................................................................................... 41 2.3.2. Selectors ......................................................................................... 43 2.3.3. Lists as Pairs .................................................................................. 44 2.3.4. Example: Association Lists as Records.......................................... 45 2.4. Polymorphic Procedures.............................................................................. 47 2.4.1. Equivalence Predicates .................................................................. 47 2.4.2. The not and null? Predicates ......................................................... 48 2.4.3. Recognition Predicates .................................................................. 49 2.4.4. Example: Searching Association Lists ........................................... 49 2.5. Meta-Procedures .......................................................................................... 51 Appendices ......................................................................................................... 53 Appendix 2.1. Mathematics in Scheme..................................................... 53 Appendix 2.2. Sequences ......................................................................... 63 Appendix 2.3. The Edit-Test-Debug Cycle .............................................. 71 Problems ............................................................................................................. 72 3. Evaluation Control and Recursion 78 3.1. Evaluation Control....................................................................................... 78 3.2. Short Circuit Evaluation .............................................................................. 79 3.3. Conditional Evaluation................................................................................ 81 3.3.1. The if-structure............................................................................... 81 3.3.2. The cond-structure ......................................................................... 84 3.3.3. Input Validation ............................................................................. 85 3.3.4. The case-structure.......................................................................... 85 3.4. Recursion..................................................................................................... 87 3.4.1. Example: Triangle Numbers .......................................................... 87 3.4.2. Tracing........................................................................................... 89 3.4.3. More on Input Validation............................................................... 90 3.4.4. Mathematical Induction ................................................................. 91 Contents xi 3.5. Thinking Recursively................................................................................... 93 3.5.1. Example: make-list......................................................................... 93 3.5.2. Example: nat-expt .......................................................................... 95 3.5.3. Example: evaluate.......................................................................... 96 Appendices ......................................................................................................... 97 Appendix 3.1. Sequential Evaluation ....................................................... 97 Appendix 3.2. Input and Output in Scheme.............................................. 98 Appendix 3.3. Defensive Programming .................................................... 104 Appendix 3.4. Debugging ......................................................................... 108 Problems ............................................................................................................. 111 4. Data Control 121 4.1. Procedure Blocks ......................................................................................... 121 4.1.1. The Nesting Instinct........................................................................ 122 4.2. The Environment Model of Eager Evaluation............................................. 126 4.2.1. Bindings ......................................................................................... 126 4.2.2. Environments ................................................................................. 127 4.2.3. Static Versus Dynamic Scope Rules ............................................... 129 4.3. Abstract Data Types .................................................................................... 132 4.3.1. Example: The CARD ADT. ............................................................ 132 4.3.2. Information Hiding and Data Abstraction ..................................... 135 4.3.3. Example: The POINT ADT ............................................................ 136 4.4. Overloading ................................................................................................. 137 4.5. Domains as Data .......................................................................................... 139 4.5.1. Programmer-Defined Types ........................................................... 140 4.5.2. Example: Complex Numbers .......................................................... 141 4.6. Data-Driven Programming .......................................................................... 145 Appendices ......................................................................................................... 147 . Appendix 4.1. Object-Oriented Programming ......................................... 147 Appendix 4.2. Expression Blocks ............................................................. 156 Problems ............................................................................................................. 159 s. Iteration 170 5.1. Modeling Systems ....................................................................................... 170 5.1.1. Iterative Evaluation ........................................................................ 171 5.1.2. Control Loops ................................................................................ 173 5.1.3. Example: A Digital Clock .............................................................. 175 5.1.4. Example: Compound Interest......................................................... 177 5.1.5. Example: A Simple Interactive System ........................................... 178 5.1.6. Example: Guess and Test ............................................................... 180 5.2. Computations as Data .................................................................................. 183 5.2.1. Predicting the Future ..................................................................... 184 xii Contents 5.2.2. Measuring Computations ............................................................... 185 5.2.3. Measuring Efficiency ..................................................................... 186 5.2.4. The Tyranny of Growth Rate .......................................................... 187 5.3. Finding Iterative Solutions .......................................................................... 187 5.4. Tail Recursion: Are do-loops Necessary? ................................................... 188 5.5. Finding Elementary Solutions ..................................................................... 190 Appendices ............. ............................................. ..... ......... ..... ............................ 191 Appendix 5.1. The Hyper-Exponential Hierarchy ................................... 191 Appendix 5.2. Undecidability .................................................................. 194 Appendix 5.3. Chaos ............................................................................... 196 Problems ............................................................................................................. 201 6. Recursive Domains 209 6.1. Recursive Domains as Hierarchies .................... .......................................... 210 6.1.1. Recursion over Hierarchies ........................................................... 211 6.2. List Recursion.. ................ ....... ....... .... ......... ......................... ........................ 211 6.2.1. Application: Are Lists Necessary? ................................................. 211 6.2.2. Application: Association Lists ........................................................ 213 6.3. The Signal Processing Paradigm ................................................................. 219 6.3.1. Filters ............................................................................................. 220 6.3.2, Amplifiers (Map) ............................................................................ 222 6.3.3. Receivers (Accumulators) .............................................................. 224 6.3.4. Transmitters (Generators) .............................................................. 226 6.3.5. Applications ................................................................................... 227 6.4. Trees and Tree Recursion ............................................................................ 228 6.4.1. Terminology ................................................................................... 229 6.4.2. The TREE Domain ......................................................................... 229 6.4.3. Tree Recursion ............................................................................... 230 6.4.4. Efficiency of Tree Recursions ......................................................... 236 Appendices ......... ....................................... ............................ ............................. 236 Appendix 6.1. Promises ........................................................................... 236 Appendix 6.2. Streams ............................................................................. 241 Problems ............................................................................................................. 246 7. Variables 257 7.1. Stores ........................................................................................................... 257 7.2. Variables and References ............................................................................. 259 7.3. Commands ................................................................................................... 261 7.4. L-Value versus R-Value ............................................................................... 263 7.5. Aliasing ........................................................................................................ 263

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.