sicp.pdf - Structure and Interpretation of Computer Programs Second Edition Unofficial Texinfo Format 2.andresraba2 Harold Abelson and Gerald Jay

sicp.pdf - Structure and Interpretation of Computer...

This preview shows page 1 out of 795 pages.

You've reached the end of your free preview.

Want to read all 795 pages?

Unformatted text preview: Structure and Interpretation of Computer Programs Second Edition Unofficial Texinfo Format 2.andresraba2 Harold Abelson and Gerald Jay Sussman with Julie Sussman foreword by Alan J. Perlis c 1996 by The Massachusetts Institute of Technology Copyright ° Structure and Interpretation of Computer Programs second edition Harold Abelson and Gerald Jay Sussman with Julie Sussman, foreword by Alan J. Perlis This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License (CC BY-SA 3.0). Based on a work at mitpress.mit.edu. The MIT Press Cambridge, Massachusetts London, England McGraw-Hill Book Company New York, St. Louis, San Francisco, Montreal, Toronto This book is one of a series of texts written by faculty of the Electrical Engineering and Computer Science Department at the Massachusetts Institute of Technology. It was edited and produced by The MIT Press under a joint production-distribution arrangement with the McGraw-Hill Book Company. Unofficial Texinfo Format 2.andresraba2 (November 21, 2011), based on 2.neilvandyke4 (January 10, 2007). 3 Short Contents Unofficial Texinfo Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Dedication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Preface to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Preface to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1 Building Abstractions with Procedures . . . . . . . . . . . . . . . . . . . . . 25 2 Building Abstractions with Data . . . . . . . . . . . . . . . . . . . . . . . . . . 118 3 Modularity, Objects and State . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 4 Metalinguistic Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 5 Computing with Register Machines . . . . . . . . . . . . . . . . . . . . . . . 625 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778 List of Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789 4 Table of Contents Unofficial Texinfo Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Dedication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Preface to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Preface to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1 Building Abstractions with Procedures . . . . . . . . . . . 25 1.1 The Elements of Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.1 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.2 Naming and the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.3 Evaluating Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.4 Compound Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.5 The Substitution Model for Procedure Application . . . . . . . . . . 1.1.6 Conditional Expressions and Predicates . . . . . . . . . . . . . . . . . . . . . 1.1.7 Example: Square Roots by Newton’s Method . . . . . . . . . . . . . . . . . 1.1.8 Procedures as Black-Box Abstractions . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Procedures and the Processes They Generate . . . . . . . . . . . . . . . . . . . . 1.2.1 Linear Recursion and Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.2 Tree Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.3 Orders of Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.4 Exponentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.5 Greatest Common Divisors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.6 Example: Testing for Primality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 Formulating Abstractions with Higher-Order Procedures . . . . . . . . 29 30 33 34 37 40 43 49 54 60 61 67 73 75 80 82 91 1.3.1 1.3.2 1.3.3 1.3.4 2 5 Procedures as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Constructing Procedures Using Lambda . . . . . . . . . . . . . . . . . . . . . . 98 Procedures as General Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Procedures as Returned Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Building Abstractions with Data . . . . . . . . . . . . . . . . . . 118 2.1 Introduction to Data Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.1 Example: Arithmetic Operations for Rational Numbers . . . . 2.1.2 Abstraction Barriers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.3 What Is Meant by Data? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.4 Extended Exercise: Interval Arithmetic . . . . . . . . . . . . . . . . . . . . . 2.2 Hierarchical Data and the Closure Property . . . . . . . . . . . . . . . . . . . . 2.2.1 Representing Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.2 Hierarchical Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.3 Sequences as Conventional Interfaces . . . . . . . . . . . . . . . . . . . . . . 2.2.4 Example: A Picture Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Symbolic Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.1 Quotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.2 Example: Symbolic Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.3 Example: Representing Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.4 Example: Huffman Encoding Trees . . . . . . . . . . . . . . . . . . . . . . . . . 2.4 Multiple Representations for Abstract Data . . . . . . . . . . . . . . . . . . . . . 2.4.1 Representations for Complex Numbers . . . . . . . . . . . . . . . . . . . . . 2.4.2 Tagged data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4.3 Data-Directed Programming and Additivity . . . . . . . . . . . . . . . . 2.5 Systems with Generic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.1 Generic Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.2 Combining Data of Different Types . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.3 Example: Symbolic Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 123 128 131 135 140 142 153 160 177 194 195 199 205 217 227 230 234 240 251 252 258 269 6 3 Modularity, Objects and State . . . . . . . . . . . . . . . . . . . . . 287 3.1 Assignment and Local State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 3.1.1 Local State Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 3.1.2 The Benefits of Introducing Assignment . . . . . . . . . . . . . . . . . . . . 297 3.1.3 The Costs of Introducing Assignment . . . . . . . . . . . . . . . . . . . . . . . 303 3.2 The Environment Model of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . 311 3.2.1 The Rules for Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 3.2.2 Applying Simple Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 3.2.3 Frames as the Repository of Local State . . . . . . . . . . . . . . . . . . . . . 319 3.2.4 Internal Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 3.3 Modeling with Mutable Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 3.3.1 Mutable List Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 3.3.2 Representing Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 3.3.3 Representing Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 3.3.4 A Simulator for Digital Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 3.3.5 Propagation of Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 3.4 Concurrency: Time Is of the Essence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 3.4.1 The Nature of Time in Concurrent Systems . . . . . . . . . . . . . . . . . 386 3.4.2 Mechanisms for Controlling Concurrency . . . . . . . . . . . . . . . . . . 392 3.5 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 3.5.1 Streams Are Delayed Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 3.5.2 Infinite Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 3.5.3 Exploiting the Stream Paradigm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 3.5.4 Streams and Delayed Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 3.5.5 Modularity of Functional Programs and Modularity of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 4 Metalinguistic Abstraction . . . . . . . . . . . . . . . . . . . . . . . . 461 4.1 The Metacircular Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.1 The Core of the Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.2 Representing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.3 Evaluator Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 467 473 483 4.1.4 Running the Evaluator as a Program . . . . . . . . . . . . . . . . . . . . . . . . 4.1.5 Data as Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.6 Internal Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.7 Separating Syntactic Analysis from Execution . . . . . . . . . . . . . . 4.2 Variations on a Scheme – Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . 4.2.1 Normal Order and Applicative Order . . . . . . . . . . . . . . . . . . . . . . . 4.2.2 An Interpreter with Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . . . 4.2.3 Streams as Lazy Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Variations on a Scheme – Nondeterministic Computing . . . . . . . . 4.3.1 Amb and Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3.2 Examples of Nondeterministic Programs . . . . . . . . . . . . . . . . . . . 4.3.3 Implementing the Amb Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4 Logic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4.1 Deductive Information Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4.2 How the Query System Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4.3 Is Logic Programming Mathematical Logic? . . . . . . . . . . . . . . . . 4.4.4 Implementing the Query System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4.4.1 The Driver Loop and Instantiation . . . . . . . . . . . . . . . . . . . . . . 4.4.4.2 The Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4.4.3 Finding Assertions by Pattern Matching . . . . . . . . . . . . . . . . 4.4.4.4 Rules and Unification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4.4.5 Maintaining the Data Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4.4.6 Stream Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4.4.7 Query Syntax Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4.4.8 Frames and Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 7 488 493 497 505 511 511 514 525 528 531 536 546 561 565 578 589 596 596 598 602 605 610 614 615 619 Computing with Register Machines . . . . . . . . . . . . . . 625 5.1 Designing Register Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1.1 A Language for Describing Register Machines . . . . . . . . . . . . . . 5.1.2 Abstraction in Machine Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1.3 Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1.4 Using a Stack to Implement Recursion . . . . . . . . . . . . . . . . . . . . . . 626 630 635 638 643 5.1.5 Instruction Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 A Register-Machine Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.1 The Machine Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.2 The Assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.3 Generating Execution Procedures for Instructions . . . . . . . . . . 5.2.4 Monitoring Machine Performance . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3 Storage Allocation and Garbage Collection . . . . . . . . . . . . . . . . . . . . . 5.3.1 Memory as Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.2 Maintaining the Illusion of Infinite Memory . . . . . . . . . . . . . . . . 5.4 The Explicit-Control Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.4.1 The Core of the Explicit-Control Evaluator . . . . . . . . . . . . . . . . . . 5.4.2 Sequence Evaluation and Tail Recursion . . . . . . . . . . . . . . . . . . . . 5.4.3 Conditionals, Assignments and Definitions . . . . . . . . . . . . . . . . . 5.4.4 Running the Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5.1 Structure of the Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5.2 Compiling Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5.3 Compiling Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5.4 Combining Instruction Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5.5 An Example of Compiled Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5.6 Lexical Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5.7 Interfacing Compiled Code to the Evaluator . . . . . . . . . . . . . . . . 8 650 651 653 658 663 672 676 677 684 692 694 702 706 709 717 721 727 736 744 748 762 767 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778 List of Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789 9 Unofficial Texinfo Format This is the second edition SICP book, from Unofficial Texinfo Format. You are probably reading it in an Info hypertext browser, such as the Info mode of Emacs. You might alternatively be reading it TEX-formatted on your screen or printer, though that would be silly. And, if printed, expensive. The freely-distributed official HTML-and-GIF format was first converted personally to Unofficial Texinfo Format (UTF) version 1 by Lytha Ayth during a long Emacs lovefest weekend in April, 2001. The UTF is easier to search than the HTML format. It is also much more accessible to people running on modest computers, such as donated ’386based PCs. A 386 can, in theory, run Linux, Emacs and a Scheme interpreter simultaneously, but most 386s probably can’t also run both Netscape and the necessary X Window System without prematurely introducing budding young underfunded hackers to the concept of thrashing. UTF can also fit uncompressed on a 1.44MB floppy diskette, which may come in handy for installing UTF on PCs that do not have Internet or LAN access. The Texinfo conversion has been a straight transliteration, to the extent possible. Like the TEX-to-HTML conversion, this was not without some introduction of breakage. In the case of Unofficial Texinfo Format, figures have suffered an amateurish resurrection of the lost art of ASCII. Also, it’s quite possible that some errors of ambiguity were introduced during the conversion of some of the copious superscripts (‘^’) and subscripts (‘ ’). Divining which has been left as an exercise to the reader. But at least we don’t put our brave astronauts at risk by encoding the greater-than-or-equal symbol as <u>&gt;</u>. If you modify ‘sicp.texi’ to correct errors or improve the ASCII art, then update the @set utfversion 2.andresraba2 line to reflect your delta. For example, if you started with Lytha’s version 1, and your name is Bob, then you could name your successive versions 1.bob1, 1.bob2, . . . 1.bobn. Also update utfversiondate. If you want to distribute your version on the Web, 10 then embedding the string “sicp.texi” somewhere in the file or Web page will make it easier for people to find with Web search engines. It is believed that the Unofficial Texinfo Format is in keeping with the spirit of the graciously freely-distributed HTML version. But you never know when someone’s armada of lawyers might need something to do, and get their shorts all in a knot over some benign little thing, so think twice before you use your full name or distribute Info, DVI, PostScript, or PDF formats that might embed your account or machine name. Peath, Lytha Ayth Addendum: See also the SICP video lectures by Abelson and Sussman: at MIT CSAIL or at MIT OCW. Second Addendum: Above is the original introduction to the UTF from 2001. Ten years later, UTF has been transformed: mathematical symbols and formulas are properly typeset, and figures drawn in vector graphics. The original text formulas and ASCII art figures are still there in the Texinfo source, but will display only when compiled to Info output. In the dawn of e-book readers and tablets, reading a PDF on screen is officially not silly anymore. Enjoy! A.R, May, 2011 11 Dedication This book is dedicated, in respect and admiration, to the spirit that lives in the computer. “I think that it’s extraordinarily important that we in computer science keep fun in computing. When it started out, it was an awful lot of fun. Of course, the paying customers got shafted every now and then, and after a while we began to take their...
View Full Document

  • Fall '15

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture