Concrete Abstractions
Total Page:16
File Type:pdf, Size:1020Kb
Out of print; available for free at http://www.gustavus.edu/+max/concrete-abstractions.html Concrete Abstractions Copyright © 1999 by Max Hailperin, Barbara Kaiser, and Karl Knight Out of print; available for free at http://www.gustavus.edu/+max/concrete-abstractions.html Copyright © 1999 by Max Hailperin, Barbara Kaiser, and Karl Knight Out of print; available for free at http://www.gustavus.edu/+max/concrete-abstractions.html Concrete Abstractions An Introduction to Computer Science Using Scheme Max Hailperin Gustavus Adolphus College Barbara Kaiser Gustavus Adolphus College Karl Knight Gustavus Adolphus College An Imprint of Brooks/Cole Publishing Company ® An International Thomson Publishing Company Paci®c Grove ² Albany ² Belmont ² Bonn ² Boston ² Cincinnati ² Detroit Johannesburg ² London ² Madrid ² Melbourne ² Mexico City ² New York Paris ² Singapore ² Tokyo ² Toronto ² Washington Copyright © 1999 by Max Hailperin, Barbara Kaiser, and Karl Knight Out of print; available for free at http://www.gustavus.edu/+max/concrete-abstractions.html Sponsoring Editor: Suzanne Jeans Cover Design: Vernon T. Boes Marketing Team: Nathan Wilbur, Michele Mootz Cover Photo: Craig Cowan. Courtesy of Couturier Gallery, Editorial Assistant: Kathryn Schooling Los Angeles, CA Production Manager: Marlene Thom Project Management and Typesetting: Manuscript Editor: Linda Thompson Integre Technical Publishing Co., Inc. Interior Design: Merry Obrecht Sawdey Printing and Binding: Webcom, Ltd. COPYRIGHT 1999 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc. The ITP logo is a registered trademark used herein under license. JavaTM is a trademark of Sun Microsystems, Inc. All other products used herein are used for identi®cation purposes only, and may be trademarks or registered trademarks of their respective owners. For more information, contact: BROOKS/COLE PUBLISHING COMPANY International Thomson Editores 511 Forest Lodge Road Seneca 53 Paci®c Grove, CA 93950 Col. Polanco USA 11560 Mexico, D.F., Mexico International Thomson Publishing Europe International Thomson Publishing GmbH Berkshire House 168±173 KonigswintererÈ Strasse 418 High Holborn 53227 Bonn London WC1V 7AA Germany England International Thomson Publishing Asia Thomas Nelson Australia 221 Henderson Road 102 Dodds Street #05±10 Henderson Building South Melbourne, 3205 Singapore 0315 Victoria, Australia International Thomson Publishing Japan Nelson Canada Hirakawacho Kyowa Building, 3F 1120 Birchmount Road 2-2-1 Hirakawacho Scarborough, Ontario Chiyoda-ku, Tokyo 102 Canada M1K 5G4 Japan All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transcribed, in any form or by any meansÐ electronic, mechanical, photocopying, recording, or otherwiseÐwithout the prior written permission of the publisher, Brooks/Cole Publishing Company, Paci®c Grove, California 93950. Printed in Canada 10987654321 Library of Congress Cataloging in Publication Data Hailperin, Max. Concrete abstractions : an introduction to computer science using Scheme / Max Hailperin, Barbara Kaiser, Karl Knight. p. cm. ISBN 0-534-95211-9 (alk. paper) IS PR 1. Computer science. 2. Abstract data types (Computer science) OK IN O TE B D S I. Kaiser, Barbara (Barbara K.). II. Knight, Karl (Karl I O H N W.). III. Title. T A C R QA76.H296 1997 E I D 0 P -F A 005.13 3Ðdc21 98-34080 R P E E ED CIP RECYCL Copyright © 1999 by Max Hailperin, Barbara Kaiser, and Karl Knight Out of print; available for free at http://www.gustavus.edu/+max/concrete-abstractions.html Contents Preface ix PART I Procedural Abstraction CHAPTER 1 Computer Science and Programming 3 1.1 What's It All About? / 3 Sidebar: Responsible Computer Use / 5 1.2 Programming in Scheme / 5 1.3 An Application: Quilting / 15 CHAPTER 2 Recursion and Induction 22 2.1 Recursion / 22 Sidebar: Exponents / 28 2.2 Induction / 28 2.3 Further Examples / 34 2.4 An Application: Custom-Sized Quilts / 40 CHAPTER 3 Iteration and Invariants 48 3.1 Iteration / 48 3.2 Using Invariants / 54 3.3 Perfect Numbers, Internal De®nitions, and Let / 58 3.4 Iterative Improvement: Approximating the Golden Ratio / 61 3.5 An Application: The Josephus Problem / 65 Copyright © 1999 by Max Hailperin, Barbara Kaiser, and Karl Knight v vi Contents CHAPTER 4 Orders of Growth and Tree Recursion 75 4.1 Orders of Growth / 75 Sidebar: Selection Sort / 77 Sidebar: Merge Sort / 78 Sidebar: Merging / 79 Sidebar: Logarithms / 82 4.2 Tree Recursion and Digital Signatures / 83 Sidebar: Modular Arithmetic / 87 4.3 An Application: Fractal Curves / 95 CHAPTER 5 Higher-Order Procedures 109 5.1 Procedural Parameters / 109 5.2 Uncomputability / 113 Sidebar: Alan Turing / 116 5.3 Procedures That Make Procedures / 118 5.4 An Application: Verifying ID Numbers / 120 PART II Data Abstraction CHAPTER 6 Compound Data and Data Abstraction 133 6.1 Introduction / 133 6.2 Nim / 135 6.3 Representations and Implementations / 143 Sidebar: Nim Program / 144 Sidebar: Game State ADT Implementation / 152 6.4 Three-Pile Nim / 153 6.5 An Application: Adding Strategies to Nim / 156 Sidebar: Type Checking / 157 CHAPTER 7 Lists 167 7.1 The De®nition of a List / 167 7.2 Constructing Lists / 169 7.3 Basic List Processing Techniques / 172 7.4 List Processing and Iteration / 179 7.5 Tree Recursion and Lists / 182 Contents vii 7.6 An Application: A Movie Query System / 187 Sidebar: Is There More to Intelligence Than the Appearance of Intelligence? / 202 CHAPTER 8 Trees 212 8.1 Binary Search Trees / 212 8.2 Ef®ciency Issues with Binary Search Trees / 220 Sidebar: Privacy Issues / 225 8.3 Expression Trees / 226 8.4 An Application: Automated Phone Books / 229 CHAPTER 9 Generic Operations 243 9.1 Introduction / 243 9.2 Multiple Representations / 244 9.3 Exploiting Commonality / 253 9.4 An Application: Computer Graphics / 262 CHAPTER 10 Implementing Programming Languages 278 10.1 Introduction / 278 10.2 Syntax / 279 Sidebar: The Expressiveness of EBNF / 285 10.3 Micro-Scheme / 289 10.4 Global De®nitions: Mini-Scheme / 303 10.5 An Application: Adding Explanatory Output / 311 PART III Abstractions of State CHAPTER 11 Computers with Memory 333 11.1 Introduction / 333 11.2 An Example Computer Architecture / 333 11.3 Programming the SLIM / 340 Sidebar: What Can Be Stored in a Location? / 342 Sidebar: SLIM's Instruction Set / 348 11.4 Iteration in Assembly Language / 349 11.5 Recursion in Assembly Language / 357 11.6 Memory in Scheme: Vectors / 361 11.7 An Application: A Simulator for SLIM / 367 viii Contents CHAPTER 12 Dynamic Programming 379 12.1 Introduction / 379 12.2 Revisiting Tree Recursion / 380 12.3 Memoization / 388 12.4 Dynamic Programming / 398 12.5 Comparing Memoization and Dynamic Programming / 406 12.6 An Application: Formatting Paragraphs / 406 CHAPTER 13 Object-based Abstractions 420 13.1 Introduction / 420 13.2 Arithmetic Expressions Revisited / 421 13.3 RA-Stack Implementations and Representation Invariants / 432 Sidebar: Strings and Characters / 433 13.4 Queues / 446 13.5 Binary Search Trees Revisited / 453 13.6 Dictionaries / 472 CHAPTER 14 Object-oriented Programming 486 14.1 Introduction / 486 14.2 An Object-oriented Program / 487 14.3 Extensions and Variations / 511 14.4 Implementing an Object-oriented Prog. System / 517 14.5 An Application: Adventures in the Land of Gack / 543 CHAPTER 15 Java, Applets, and Concurrency 577 15.1 Introduction / 577 15.2 Java / 578 15.3 Event-Driven Graphical User Interfaces in Applets / 599 15.4 Concurrency / 616 Sidebar: Nested Calls to Synchronized Methods and Deadlock / 625 15.5 An Application: Simulating Compound Interest / 632 APPENDIX Nonstandard Extensions to Scheme 645 Bibliography 649 Index 653 Preface At ®rst glance, the title of this book is an oxymoron. After all, the term abstraction refers to an idea or general description, divorced from physical objects. On the other hand, something is concrete when it is a particular object, perhaps something that you can manipulate with your hands and look at with your eyes. Yet you often deal with concrete abstractions. Consider, for example, a word processor. When you use a word processor, you probably think that you have really entered a document into the computer and that the computer is a machine which physically manipulates the words in the document. But in actuality, when you ªenterº the document, there is nothing new inside the computerÐthere are just different patterns of activity of electrical charges bouncing back and forth. Moreover, when the word processor ªmanipulatesº the words in the document, those manipulations are really just more patterns of electrical activity. Even the program that you call a ªword processorº is an abstractionÐit's the way we humans choose to talk about what is, in reality, yet more electrical charges. Still, although these abstractions such as ªword processorsº and ªdocumentsº are merely convenient ways of describing patterns of electrical activity, they are also things that we can buy, sell, copy, and use. As you read through this book, we will introduce several abstract ideas in as concrete a way as possible. As you become familiar and comfortable with these ideas, you will begin to think of the abstractions as actual concrete objects. Having already gone through this process ourselves, we've chosen to call computer science ªthe discipline of concrete abstractionsº; if that seems too peculiar to fathom, we invite you to read the book and then reconsider the notion. This book is divided into three parts, dealing with procedural abstractions, data abstractions, and abstractions of state. A procedure is a way of abstracting what's called a computational process. Roughly speaking, a process is a dynamic succession of eventsÐa happening. When your computer is busy doing something, a process ix x Preface is going on inside it. When we call a process a computational process, we mean that we are ignoring the physical nature of the process and instead focusing on the information content. For example, consider the problem of conveying some information to a bunch of other people.