ebook img

Thinking Recursively PDF

190 Pages·1986·4.365 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 Thinking Recursively

THINKING THINKING THINKING THINKING THINKING lIU\;KI:-;O I-~.I U....I..O ~~~ R}-(1"}I;,\IVl-lY R[Cl'R~IV[LY RECURSIVELY RECURSIVELY RECURSIVELY RECURSIVELY RECURSIVELY eng Thi Recursively ERIC ROBERTS Department ofComputer Science Wellesley College. Wellesley. Mass. JOHN WILEY & SONS, INC. New York' Chichester' Brisbane' Toronto' Singapore Copyright © 1986. byJohn Wiley & Sons. Inc. All rights reserved. Published simultaneously inCandda Reproduction ortran~lationofany part ofthis work beyond that permitted by Sections 107or 108ofthe 1976 United States Copyright Act without the permi'i~ionofthe copyright owneri~ unlawful Reque~hfor permi~sionor further information ~hould be addre~,edto the Permi~,ion' Department. John Wiley & Son~. Inc. LibraryofConWl'S.\' Cata/oKinK in Pub/iclition Data Robert~. Eric. Thinking recur~ively. Bibliography p Includes index. I. Recur~ion theory. I. Title QA9.6.R63 19115 5113 85-2036~ ISBN 0-471-81652-3 Printed in the United States ofAmerica 10 9 II 7 6 5 4 l 2 I In loving memory of my grandmother RUTH STENIUS ROBERTS (1899-1982) Preface In myexperience. teachingstudentsto use recursion hasalwaysbeenadifficult task. When it is first presented. students often react with a certain suspicion to the entire idea. as if they had just been exposed to some conjurer's trick rather than a new programming methodology. Given that reaction. many stu dents neverlearn toapply recursive techniques and proceedto more advanced courses unable to write programs which depend on the use ofrecursive strat egies. This book is intended to demystify this material and encourage the student to "think recursively." This book is intended for use as a supplementary text in an intermediate course in data structures. but it could equally well be used with many other coursesat this level. Theonly prerequisitefor usingthis text isan introductory programming course. Since Pascal is used in the programming examples. the student must also become familiar with Pascal programming. although this can easily be included as part ofthe same course. To support the concurrent pre sentation ofPascal and the material on recursion. the programming examples in the early chapters require only the most basic features ofPascal. In order to develop a more complete understanding of the topic. it is important for the student toexamine recursion from several different perspec tives. Chapter I provides an informal overview which examines the use of recursion outside the context ofprogramming. Chapter 2examines the under lying mathematical concepts and helps the student develop an appropriate conceptual model. In particular. this chapter covers mathematical induction and computational complexity in considerable detail. This discussion is de signed to be nonthreatening to the math-anxious student and. at the same time, include enough formal structure to emphasize the extent to which computer science depends on mathematics for its theoretical foundations. Chapter 3 applies the technique of recursive decomposition to various mathematical functions and begins to show how recursion is represented in Pascal. Chapter 4 continues this discussion in the context ofrecursive proce- vii viii Preface dures, emphasizingthe parallel between recursive decompositionand the more familiar technique ofstepwise refinement. Chapters 5 through 9 present several examples ofthe use ofrecursion to solveincreasingly sophisticatedproblems. Chapter7isofparticularimportance and covers recursive sorting techniques, illustrating the applicability of the recursion methodology to practical problem domains. Chapter9contains many delightful examples, which make excellent exercises and demonstrations if graphical hardware is available. Chapter 10 examines the use of recursive procedures in the context of recursive data structures and contains several important examples. Structur ally, thischapterappearslateinthe text primarilyto avoid introducing pointers prematurely. Forcourses in which the students have been introducedto point ers relatively early, it may be useful to cover the material in Chapter 10 im mediately after Chapter 7. Finally, Chapter II examines the underlying implementation ofrecursion and provides the final link in removing the mystery. On the other hand, this materialis notessentialto the presentationand mayinterfere with the student's conceptual understanding ifpresented too early. I am deeply grateful for the assistance of many people who have helped to shape the final form ofthe text. I want to express a special note ofappre ciation to Jennifer Friedman, whose advice has been invaluable in matters of both substance and style. I would also like to thank my colleagues on the Wellesley faculty, Douglas Long, K. Wojtek Przytula, Eleanor Lonske, James Finn, Randy Shull, and Don Wolitzer for their support. Joe Buhler at Reed College, Richard Pattis at the University of Washington, Gary Ford at the University ofColorado at Colorado Springs, Steve Berlin at M.LT., and Suz anne Rodday (Wellesley '85) have all made important suggestions that have dramatically improved the final result. Eric Roberts Contents Preface ix 1 The Idea of Recursion 1 I-I An Illustration ofthe Recursive Approach / 1-2 Mondrian and Computer Art 4 1-3 Characteristics of Recursive Algorithms 7 1-4 Nonterminating Recursion 8 1-5 Thinking about Recursion-Two Perspectives 9 2 Mathematical Preliminaries 13 2-1 Mathematical Induction /4 2-2 Computational Complexity /9 3 Recursive Functions 31 3-1 Functional vs. Procedural Recursion 32 3-2 Factorials 32 3-3 The Fibonacci Sequence 36 4 The Procedural Approach 47 4-1 Numeric Output 49 4-2 Generating a Primer 54 ix

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.