How to Design Programs phần 1 pot

57 290 0
How to Design Programs phần 1 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

-1- How to Design Programs An Introduction to Computing and Programming Matthias Felleisen Robert Bruce Findler Matthew Flatt Shriram Krishnamurthi The MIT Press Cambridge, Massachusetts London, England Book Description This introduction to programming places computer science in the core of a liberal arts education. Unlike other introductory books, it focuses on the program design process. This approach fosters a variety of skills critical reading, analytical thinking, creative synthesis, and attention to detail- -that are important for everyone, not just future computer programmers. The book exposes readers to two fundamentally new ideas. First, it presents program design guidelines that show the reader how to analyze a problem statement; how to formulate concise goals; how to make up examples; how to develop an outline of the solution, based on the analysis; how to finish the program; and how to test. Each step produces a well-defined intermediate product. Second, the book comes with a novel programming environment, the first one explicitly designed for beginners. The environment grows with the readers as they master the material in the book until it supports a full-fledged language for the whole spectrum of programming tasks. All the book's support materials are available for free on the Web. The Web site includes the environment, teacher guides, exercises for all levels, solutions, and additional projects. TEAMFLY TEAM FLY PRESENTS -2- Contents Preface Why Everyone Should Learn to Program Design Recipes The Choice of Scheme and DrScheme The Parts of the Book Acknowledgments I Processing Simple Forms of Data 1 Students, Teachers, and Computers 2 Numbers, Expressions, Simple Programs 2.1 Numbers and Arithmetic 2.2 Variables and Programs 2.3 Word Problems 2.4 Errors 2.5 Designing Programs 3 Programs are Function Plus Variable Definitions 3.1 Composing Functions 3.2 Variable Definitions 3.3 Finger Exercises on Composing Functions 4 Conditional Expressions and Functions 4.1 Booleans and Relations 4.2 Functions that Test Conditions 4.3 Conditionals and Conditional Functions 4.4 Designing Conditional Functions 5 Symbolic Information 5.1 Finger Exercises with Symbols 6 Compound Data, Part 1: Structures 6.1 Structures 6.2 Extended Exercise: Drawing Simple Pictures 6.3 Structure Definitions 6.4 Data Definitions 6.5 Designing Functions for Compound Data 6.6 Extended Exercise: Moving Circles and Rectangles 6.7 Extended Exercise: Hangman 7 The Varieties of Data 7.1 Mixing and Distinguishing Data 7.2 Designing Functions for Mixed Data 7.3 Composing Functions, Revisited TEAMFLY TEAM FLY PRESENTS -3- 7.4 Extended Exercise: Moving Shapes 7.5 Input Errors 8 Intermezzo 1: Syntax and Semantics 8.2 The Scheme Vocabulary 8.3 The Scheme Grammar 8.4 The Meaning of Scheme 8.5 Errors 8.6 Boolean Expressions 8.7 Variable Definitions 8.8 Structure Definitions II Processing Arbitrarily Large Data 9 Compound Data, Part 2: Lists 9.1 Lists 9.2 Data Definitions for Lists of Arbitrary Length 9.3 Processing Lists of Arbitrary Length 9.4 Designing Functions for Self-Referential Data Definitions 9.5 More on Processing Simple Lists 10 More on Processing Lists 10.1 Functions that Produce Lists 10.2 Lists that Contain Structures 10.3 Extended Exercise: Moving Pictures 11 Natural Numbers 11.1 Defining Natural Numbers 11.2 Processing Natural Numbers of Arbitrary Size 11.3 Extended Exercise: Creating Lists, Testing Functions 11.4 Alternative Data Definitions for Natural Numbers 11.5 More on the Nature of Natural Numbers 12 Composing Functions, Revisited Again 12.1 Designing Complex Programs 12.2 Recursive Auxiliary Functions 12.3 Generalizing Problems, Generalizing Functions 12.4 Extended Exercise: Rearranging Words 13 Intermezzo 2: List Abbreviations III More on Processing Arbitrarily Large Data 14 More Self-referential Data Definitions 14.1 Structures in Structures 14.2 Extended Exercise: Binary Search Trees 14.3 Lists in Lists 14.4 Extended Exercise: Evaluating Scheme TEAMFLY TEAM FLY PRESENTS -4- 15 Mutually Referential Data Definitions 15.1 Lists of Structures, Lists in Structures 15.2 Designing Functions for Mutually Referential Definitions 15.3 Extended Exercise: More on Web Pages 16 Development through Iterative Refinement 16.1 Data Analysis 16.2 Defining Data Classes and Refining Them 16.3 Refining Functions and Programs 17 Processing Two Complex Pieces of Data 17.1 Processing Two Lists Simultaneously: Case 1 17.2 Processing Two Lists Simultaneously: Case 2 17.3 Processing Two Lists Simultaneously: Case 3 17.4 Function Simplification 17.5 Designing Functions that Consume Two Complex Inputs 17.6 Exercises on Processing Two Complex Inputs 17.7 Extended Exercise: Evaluating Scheme, Part 2 17.8 Equality and Testing 18 Intermezzo 3: Local Definitions and Lexical Scope 18.2 Organizing Programs with local Syntax of local Semantics of local Pragmatics of local, Part 1 Pragmatics of local, Part 2 Pragmatics of local, Part 3 18.3 Lexical Scope and Block Structure IV Abstracting Designs 19 Similarities in Definitions 19.1 Similarities in Functions 19.2 Similarities in Data Definitions 20 Functions are Values 20.1 Syntax and Semantics 20.2 Contracts for Abstract and Polymorphic Functions 21 Designing Abstractions from Examples 21.1 Abstracting from Examples 21.2 Finger Exercises with Abstract List Functions 21.3 Abstraction and a Single Point of Control 21.4 Extended Exercise: Moving Pictures, Again 21.5 Note: Designing Abstractions from Templates 22 Designing Abstractions with First-Class Functions 22.1 Functions that Produce Functions 22.2 Designing Abstractions with Functions-as-Values 22.3 A First Look at Graphical User Interfaces TEAMFLY TEAM FLY PRESENTS -5- 23 Mathematical Examples 23.1 Sequences and Series 23.2 Arithmetic Sequences and Series 23.3 Geometric Sequences and Series Taylor Series 23.4 The Area Under a Function 23.5 The Slope of a Function 24 Intermezzo 4: Defining Functions on the Fly Syntax of lambda Scope and Semantics of lambda Pragmatics of lambda V Generative Recursion 25 A New Form of Recursion 25.1 Modeling a Ball on a Table 25.2 Sorting Quickly 26 Designing Algorithms 26.1 Termination 26.2 Structural versus Generative Recursion 26.3 Making Choices 27 Variations on a Theme 27.1 Fractals 27.2 From Files to Lines, from Lists to Lists of Lists 27.3 Binary Search 27.4 Newton's Method 27.5 Extended Exercise: Gaussian Elimination 28 Algorithms that Backtrack 28.1 Traversing Graphs 28.2 Extended Exercise: Checking (on) Queens 29 Intermezzo 5: The Cost of Computing and Vectors 29.2 Concrete Time, Abstract Time 29.3 The Definition of ``on the Order of'' 29.4 A First Look at Vectors VI Accumulating Knowledge 30 The Loss of Knowledge 30.1 A Problem with Structural Processing 30.2 A Problem with Generative Recursion 31 Designing Accumulator-Style Functions 31.1 Recognizing the Need for an Accumulator 31.2 Accumulator-Style Functions 31.3 Transforming Functions into Accumulator-Style TEAMFLY TEAM FLY PRESENTS -6- 32 More Uses of Accumulation 32.1 Extended Exercise: Accumulators on Trees 32.2 Extended Exercise: Missionaries and Cannibals 32.3 Extended Exercise: Board Solitaire 33 Intermezzo 6: The Nature of Inexact Numbers 33.2 Fixed-size Number Arithmetic 33.3 Overflow 33.4 Underflow 33.5 DrScheme's Numbers VII Changing the State of Variables 34 Memory for Functions 35 Assignment to Variables 35.1 Simple Assignments at Work 35.2 Sequencing Expression Evaluations 35.3 Assignments and Functions 35.4 A First Useful Example 36 Designing Functions with Memory 36.1 The Need for Memory 36.2 Memory and State Variables 36.3 Functions that Initialize Memory 36.4 Functions that Change Memory 37 Examples of Memory Usage 37.1 Initializing State 37.2 State Changes from User Interactions 37.3 State Changes from Recursion 37.4 Finger Exercises on State Changes 37.5 Extended Exercise: Exploring Places 38 Intermezzo 7: The Final Syntax and Semantics 38.2 The Vocabulary of Advanced Scheme 38.3 The Grammar of Advanced Scheme 38.4 The Meaning of Advanced Scheme 38.5 Errors in Advanced Scheme VIII Changing Compound Values 39 Encapsulation 39.1 Abstracting with State Variables 39.2 Practice with Encapsulation 40 Mutable Structures 40.1 Structures from Functions 40.2 Mutable Functional Structures 40.3 Mutable Structures TEAMFLY TEAM FLY PRESENTS -7- 40.4 Mutable Vectors 40.5 Changing Variables, Changing Structures 41 Designing Functions that Change Structures 41.1 Why Mutate Structures 41.2 Structural Design Recipes and Mutation, Part 1 41.3 Structural Design Recipes and Mutation, Part 2 41.4 Extended Exercise: Moving Pictures, a Last Time 42 Equality 42.1 Extensional Equality 42.2 Intensional Equality 43 Changing Structures, Vectors, and Objects 43.1 More Practice with Vectors 43.2 Collections of Structures with Cycles 43.3 Backtracking with State Epilogue Computing Programming Moving On Index TEAMFLY TEAM FLY PRESENTS -8- Preface It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical? Alan Perlis, Epigrams in Programming Many professions require some form of computer programming. Accountants program spreadsheets and word processors; photographers program photo editors; musicians program synthesizers; and professional programmers instruct plain computers. Programming has become a required skill. Yet programming is more than just a vocational skill. Indeed, good programming is a fun activity, a creative outlet, and a way to express abstract ideas in a tangible form. And designing programs teaches a variety of skills that are important in all kinds of professions: critical reading, analytical thinking, creative synthesis, and attention to detail. We therefore believe that the study of program design deserves the same central role in general education as mathematics and English. Or, put more succinctly, everyone should learn how to design programs. On one hand, program design teaches the same analytical skills as mathematics. But, unlike mathematics, working with programs is an active approach to learning. Interacting with software provides immediate feedback and thus leads to exploration, experimentation, and self-evaluation. Furthermore, designing programs produces useful and fun things, which vastly increases the sense of accomplishment when compared to drill exercises in mathematics. On the other hand, program design teaches the same analytical reading and writing skills as English. Even the smallest programming tasks are formulated as word problems. Without critical reading skills, a student cannot design programs that match the specification. Conversely, good program design methods force a student to articulate thoughts about programs in proper English. The Design Recipe for Functions Problem Analysis & Data Definition Contract, Purpose & Effect Statements, Header Examples Function Template Function Definition Tests Figure 1: The basic steps of a program design recipe This book is the first book on programming as the core subject of a liberal arts education. Its main focus is the design process that leads from problem statements to well-organized solutions; TEAMFLY TEAM FLY PRESENTS -9- it deemphasizes the study of programming language details, algorithmic minutiae, and specific application domains. Our desire to focus on the design process requires two radical innovations for introductory courses. The first innovation is a set of explicit design guidelines. Existing curricula tend to provide vague and ill-defined suggestions, such as ``design from top to bottom'' or ``make the program structural.'' We have instead developed design guidelines that lead students from a problem statement to a computational solution in step-by-step fashion with well- defined intermediate products. In the process they learn to read, to analyze, to organize, to experiment, to think in a systematic manner. The second innovation is a radically new programming environment. In the past, texts on programming ignored the role of the programming environment in the learning process; they simply assumed that students had access to a professional environment. This book provides a programming environment for beginners. It also grows with the students as they master more and more of the material until it supports a full- fledged language for the whole spectrum of programming tasks: large-scale programming as well as scripting. Our guidelines are formulated as a number of program design recipes. 1 A design recipe guides a beginning programmer through the entire problem-solving process. With design recipes, a beginner almost never again stares at a blank piece of paper or a blank computer screen. Instead, the student will check the design recipe and use the question-and-answer guidelines to make some progress. We created the design recipes by identifying categories of problems. The identification of a problem category is based on the classes of data that are used to represent the relevant information. Starting from the structure of this class description students derive the programs with a checklist. Figure 1 shows the basic six steps of a design recipe checklist. Each step produces a well-defined intermediate product: 1. the description of the class of problem data; 2. the informal specification of a program's behavior; 3. the illustration of the behavior with examples; 4. the development of a program template or layout; 5. the transformation of the template into a complete definition; and 6. the discovery of errors through testing. The major difference concerns the relationship of steps 1 and 4. Design recipes help beginners and teachers alike. Teachers can use the recipes to inspect a beginner's problem-solving skills, to diagnose weaknesses, and to suggest specific remedial steps. After all, each stage of the design recipe yields a well-defined, checkable product. If a beginner is stuck, a teacher can inspect the intermediate products and determine what the problem is. Based on this analysis, the teacher can then provide guidance for a specific step in the recipe, raise appropriate questions, and recommend additional practice exercises. Why Everyone Should Learn to Program And as imagination bodies forth The forms of things to unknown, and the poet's pen Turns them to shapes, and gives to airy nothing A local habitation and a name. TEAMFLY TEAM FLY PRESENTS -10- Shakespeare, A Midsummer Night's Dream V(i) Our claim that everyone programs or should learn to program might appear strange considering that, at first glance, fewer and fewer people seem to program these days. Instead, the majority of people use application packages, which don't seem to require any programming. Even programmers use ``program generators,'' packages that create programs from, say, business rules. So why should anyone learn to program? The answer consists of two parts. First, it is indeed true that traditional forms of programming are useful for just a few people. But, programming as we the authors understand it is useful for everyone: the administrative secretary who uses spreadsheets as well as the high-tech programmer. In other words, we have a broader notion of programming in mind than the traditional one. We explain our notion in a moment. Second, we teach our idea of programming with a technology that is based on the principle of minimal intrusion. Hence our notion of programming teaches problem-analysis and problem-solving skills without imposing the overhead of traditional programming notations and tools. To get a better understanding of modern programming, take a closer look at spreadsheets, one of today's popular application packages. A user enters formulas into a spreadsheet. The formulas describe how a cell A depends on another cell B. Then, as the user enters a number into B, the spreadsheet automatically calculates the contents of cell A. For complicated spreadsheets, a cell may depend on many other cells, not just one. Other application packages require similar activities. Consider word processors and style sheets. A style sheet specifies how to create a (part of a) document from yet-to-be-determined words or sentences. When someone provides specific words and a style sheet, the word processor creates the document by replacing names in the style sheet with specific words. Similarly, someone who conducts a Web search may wish to specify what words to look for, what words should be next to each other, and what words should not occur in the page. In this case, the output depends on the search engine's cache of Web pages and the user's search expression. Finally, using a program generator in many ways relies on the same skills as those necessary for application packages. A program generator creates a program in a traditional programming language, such as C++ or Java, from high-level descriptions, such as business rules or scientific laws. Such rules typically relate quantities, sales, and inventory records and thus specify computations. The other parts of the program, especially how it interacts with a user and how it stores data in the computer's disk, are generated with little or no human intervention. All of these activities instruct some computer software to do something for us. Some use scientific notation, some may use stylized English, some use a concrete programming notation. All of them are some form of programming. The essence of these activities boils down to two concepts: 1. relating one quantity to another quantity, and 2. evaluating a relationship by substituting values for names. Indeed, the two concepts characterize programming at the lowest level, the computer's native language, and in a modern fashionable language such as Java. A program relates its inputs to outputs; and, when a program is used for specific inputs, the evaluation substitutes concrete values for names. TEAMFLY TEAM FLY PRESENTS [...]... Craig Claiborne (19 20-2000), Food Editor, New York Times Learning to design programs is like learning to play soccer A player must learn to trap a ball, to dribble with a ball, to pass, and to shoot a ball Once the player knows those basic skills, the next goals are to learn to play a position, to play certain strategies, to choose among feasible strategies, and, on occasion, to create variations... evaluates to the same result, no matter how often we evaluate it This property makes it easy to design, and to reason about, programs To cope with interfaces between programs and the rest of the world, however, we enrich the language with assignment statements and abandon some of our algebraic reasoning The last two parts show what this means for the design of programs More precisely, they show how the design. .. that shows how to compute the number of attendees from the ticket price Make up more examples if needed -35TEAM FLY PRESENTS Exercise 3 .1. 2 Use the results of exercise 3 .1. 1 to determine how much it costs to run a show at $3.00, $4.00, and $5.00 Also determine how much revenue each show produces at those prices Finally, figure out how much profit the monopolistic movie owner can make with each show Which... refers to DrScheme hints; they are available in both versions of the book The programming environment has been designed with students in mind The hints suggest how to use DrScheme at the various stages of the learning process This marker refers to teacher hints, which suggest strategies on how to present a section, on how to approach an exercise, or on how to supplement some material This marker links to. .. ticket-price) (+ 12 0 (* (/ 15 10 ) (- 5.00 ticketprice)))) ;; How not to design a program (define (profit price) (- (* (+ 12 0 (* (/ 15 10 ) (- 5.00 price))) price) (+ 18 0 (* 04 (+ 12 0 (* (/ 15 10 ) (- 5.00 price))))))) Figure 5: Two ways to express the profit program Instead of developing a function per dependency in the problem statement, we could have tried to express the relationship between the ticket price... dollars is 12 0, and for each 10 cents less than five dollars, 15 more attendees show up ;; How to design a program (define (profit ticket-price) (- (revenue ticket-price) (cost ticket-price))) Y L F M (define (revenue ticket-price) (* (attendees ticket-price) ticketprice)) (define (cost ticket-price) (+ 18 0 (* 04 (attendees ticketprice)))) A E T (define (attendees ticket-price) (+ 12 0 (* (/ 15 10 ) (- 5.00... data-driven program design, and it is the most frequently used form of design Data-driven designs are easy to create, easy to understand, and easy to extend and modify Other design recipes introduce the notion of generative recursion, accumulation, and history sensitivity The first one produces recursive programs that generate new instances of problems as they recur; accumulator-style programs collect... cannot be created by tinkering around It must be carefully designed Each piece needs a lot of attention; composing programs into larger units must follow a well-planned strategy Designing programs properly must be practiced from our very first day of programming In this book, we will learn to design computer programs, and we will learn to understand how they function Becoming and being a programmer is fun,... and more knowledge about how to organize programs Y L F M Adding pieces of functionality to a program demonstrates why programmers must follow a design discipline Solving the problem again shows students how to choose from alternative design recipes Finally, on occasion, new knowledge just helps students improve the program organization; in other words, students learn that programs aren't finished... -5 2/3 17 /3 #i1. 414 213 56237 31 The first is an integer, the second one a negative integer, the next two are fractions, and the last one is an inexact representation of a real number A E T Like a pocket calculator, the simplest of computers, Scheme permits programmers to add, subtract, multiply, and divide numbers: (+ 5 5) (+ -5 5) (+ 5 -5) (- 5 5) (* 3 4) (/ 8 12 ) The first three ask Scheme to perform . that show the reader how to analyze a problem statement; how to formulate concise goals; how to make up examples; how to develop an outline of the solution, based on the analysis; how to finish. Simple Lists 10 More on Processing Lists 10 .1 Functions that Produce Lists 10 .2 Lists that Contain Structures 10 .3 Extended Exercise: Moving Pictures 11 Natural Numbers 11 .1 Defining. Vectors 40.5 Changing Variables, Changing Structures 41 Designing Functions that Change Structures 41. 1 Why Mutate Structures 41. 2 Structural Design Recipes and Mutation, Part 1 41. 3

Ngày đăng: 12/08/2014, 19:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan