part to teach algebraic problem solving from a computational perspective; and yet others worked through the entire book in a year. For more information on schedules, visit the book,s Web site THE BOOK ON THE WEB: The book comes in two versions: a paper copy and a freely accessible on- line version at http://www.htdp.org The Web site also provides additional material, especially extended exercises of the style mentioned above. At this time, the Web page offers exercises on the visual simulation of ball games and the management of Web site. More exercises will be added The two versions of the book come with different kinds of hints each is marked with one of the following three icons This marker 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 on-line solutions. Some solutions are freely available; others are rs? accessible to registered teachers only. To find out more about registration, see the book,s Web site TYPOGRAPHY AND DEFINITIONS: For readability. Scheme programs are typeset using a small number of fonts. Italic words refer to program hames and variables. Sans Serif items are constants and built-in operations. Boldface words are Scheme keywords Definitions come in three varieties. There are those terms that concern the principles of programming and computing. The book lists the first occurrence of such terms with SMALL CAPITAL LETTERS. Other definitions are of a more fleeting nature; they introduce terms that are important for a section, an example, an exercise, or some other small part of the book. The book uses data definitions are boxed, and the first occurrence of the defined name is also typeset usinlg ost slanted words to emphasize such definitions. Finally, the book also defines classes of data. M slanted word Acknowledgments Four people deserve special thanks: Robert" Corky"Cartwright, who co-developed a predecessor of Rice's introductory course with the first author; Daniel P. Friedman, for asking the first author to rewrite The little lisper (also MIT Press)in 1984, because it started this project; John Clements, who designed, implemented, and maintains DrScheme's stepper; and Paul Steckler, who faithfully supported the team with contributions to our suite of programming tools The develo nent of the book benefited from many other friends and colleagues who used it in their courses and/or gave detailed comments on early drafts. We are grateful to them for their 16- TEAM FLY PRESENTS
-16- part to teach algebraic problem solving from a computational perspective; and yet others worked through the entire book in a year. For more information on schedules, visit the book's Web site. THE BOOK ON THE WEB: The book comes in two versions: a paper copy and a freely accessible online version at http://www.htdp.org/ The Web site also provides additional material, especially extended exercises of the style mentioned above. At this time, the Web page offers exercises on the visual simulation of ball games and the management of Web site. More exercises will be added. The two versions of the book come with different kinds of hints. Each is marked with one of the following three icons: This marker 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 on-line solutions. Some solutions are freely available; others are accessible to registered teachers only. To find out more about registration, see the book's Web site. TYPOGRAPHY AND DEFINITIONS: For readability, Scheme programs are typeset using a small number of fonts. Italic words refer to program names and variables. Sans Serif items are constants and built-in operations. Boldface words are Scheme keywords. Definitions come in three varieties. There are those terms that concern the principles of programming and computing. The book lists the first occurrence of such terms with SMALL CAPITAL LETTERS. Other definitions are of a more fleeting nature; they introduce terms that are important for a section, an example, an exercise, or some other small part of the book. The book uses slanted words to emphasize such definitions. Finally, the book also defines classes of data. Most data definitions are boxed, and the first occurrence of the defined name is also typeset using slanted words. Acknowledgments Four people deserve special thanks: Robert ``Corky'' Cartwright, who co-developed a predecessor of Rice's introductory course with the first author; Daniel P. Friedman, for asking the first author to rewrite The Little LISPer (also MIT Press) in 1984, because it started this project; John Clements, who designed, implemented, and maintains DrScheme's stepper; and Paul Steckler, who faithfully supported the team with contributions to our suite of programming tools. The development of the book benefited from many other friends and colleagues who used it in their courses and/or gave detailed comments on early drafts. We are grateful to them for their TEAMFLY TEAM FLY PRESENTS
help and their patience: lan Barland, John Clements, Bruce Duba, Mike Ernst, Kathi Fisler Daniel P. Friedman, John Greiner, John Stone Geraldine morin, and valdemar tamez a dozen generations of Comp 210 students at Rice University used early drafts of the text and contributed improvements in various ways. In addition, numerous attendees of our TeachScheme! workshops used early drafts in their classrooms. Many sent in comments and suggestions. As representative of these we mention the following active contributors: Ms. Barbara Adler, Dr Stephen Bloch, Mr Jack Clay, Dr. Richard Clemens, Mr. Kyle Gillette, Ms Karen Buras, Mr Marvin Hernandez, Mr Michael Hunt, Ms Karen North, Mr. Jamie Raymond, and Mr Robert Reid. Christopher Felleisen patiently worked through the first few parts of the book with his father and provided direct insight into the views of a young student. Hrvoje blazevic(Master of LPG/C Harriette), Joe Zachary (University of Utah)and Daniel P. Friedman (Indiana University) discovered numerous typos in the first printing, which we have now fixed. Thank you to everyone Finally, Matthias expresses his gratitude to Helga for her many years of patience and for creating a home for an absent-minded husband and father. Robby is grateful to Hsing- Huei Huang for her support and encouragement; without her, he would not have gotten anything done. Matthew thanks Wen Yuan for her constant support and enduring music. Shriram is indebted to Kathi Fisler for support, patience and puns, and for her participation in this project I Readers whose experience is exclusively based on programming languages such as C/C++,w Basic, and Pascal should read procedure"or 'method"where the preface mentions"program 2 Our design recipes were inspired by work with Daniel P. Friedman on structural recursion, with Robert Harper on type theory, and by Michael A. Jackson,'s design method 3 Scheme official definition--the Revised report on Scheme, edited by richard Kelsey, William and Jonathan Rees--and many implementations For a copy of the report and foralistofalternativeSchemeimplementationsvisitwww.schemersorgontheWeb.Note however, that the language of this book extends that of the report and is tailored to beginners 1 TEAM FLY PRESENTS
-17- help and their patience: Ian Barland, John Clements, Bruce Duba, Mike Ernst, Kathi Fisler, Daniel P. Friedman, John Greiner, John Stone, Geraldine Morin, and Valdemar Tamez. A dozen generations of Comp 210 students at Rice University used early drafts of the text and contributed improvements in various ways. In addition, numerous attendees of our TeachScheme! workshops used early drafts in their classrooms. Many sent in comments and suggestions. As representative of these we mention the following active contributors: Ms. Barbara Adler, Dr. Stephen Bloch, Mr. Jack Clay, Dr. Richard Clemens, Mr. Kyle Gillette, Ms. Karen Buras, Mr. Marvin Hernandez, Mr. Michael Hunt, Ms. Karen North, Mr. Jamie Raymond, and Mr. Robert Reid. Christopher Felleisen patiently worked through the first few parts of the book with his father and provided direct insight into the views of a young student. Hrvoje Blazevic (Master of LPG/C Harriette), Joe Zachary (University of Utah) and Daniel P. Friedman (Indiana University) discovered numerous typos in the first printing, which we have now fixed. Thank you to everyone. Finally, Matthias expresses his gratitude to Helga for her many years of patience and for creating a home for an absent-minded husband and father. Robby is grateful to Hsing-Huei Huang for her support and encouragement; without her, he would not have gotten anything done. Matthew thanks Wen Yuan for her constant support and enduring music. Shriram is indebted to Kathi Fisler for support, patience and puns, and for her participation in this project. 1 Readers whose experience is exclusively based on programming languages such as C/C++, Basic, and Pascal should read ``procedure'' or ``method'' where the preface mentions ``program.'' 2 Our design recipes were inspired by work with Daniel P. Friedman on structural recursion, with Robert Harper on type theory, and by Michael A. Jackson's design method. 3 Scheme has an official definition -- the Revised Report on Scheme, edited by Richard Kelsey, William Clinger, and Jonathan Rees -- and many implementations. For a copy of the report and for a list of alternative Scheme implementations, visit www.schemers.org on the Web. Note, however, that the language of this book extends that of the report and is tailored to beginners. TEAMFLY TEAM FLY PRESENTS
Part I Processing Simple Forms of Data -18- Flyheart.com7f TEAM FLY PRESENTS
-18- Part I Processing Simple Forms of Data TEAMFLY TEAM FLY PRESENTS
Section 1 Students, Teachers, and Computers We learn to compute at a young age At first we just add and subtract numbers One plus one equals two. Five minus two is three As we grow older we learn about additional mathematical operations, like exponentiation and sine, but we also learn to describe rules of computation Given a circle of radius r, its circumference is r times two times pi. A minimum-wage laborer who works for n hours earns N times 5.35 dollars The truth is, our teachers turn us into computers and program us to execute simple computer programs o, the secret is out. Computer programs are just very fast students. They can perform millions of additions while we might still be struggling with the first one But computer programs can de more than just manipulate numbers. They can guide lare. They-can play games. They can look up a person s phone number. They can print thei payroll checks for huge corporations.In short,computers process all kinds of information People state information and instructions in English The temperature is 35 e connett this temperature into Fahrenheit. It takes this car 35 seconds to accelerate from zero to NOQ miles perhour; determine how far the car gets in 20 seconds Computers, however, barely understand basic English and certainly can't understand complex instructions expressed in English. Instead we must learn to speak a computer language so that we can communicate information and instructions A computers language of instruction and information is a PROGRAMMING LANGUAGE Information expressed in a programming language is called DATA. There are many flavors of data. Numbers are one class of data Number series belong to the class of comPound DATA because each series made up of other pieces of smaller pieces of data, namely, numbers. To contrast the two kinds of data, we also call numbers ATOMIC DATA. Letters are other examples of atomic data; family trees are compound data Data represents information, but the concrete interpretation is up to us. For example, a number ke 37.51 may represent a temperature, a time, or a distance. a letter like A"may denote a school grade, a quality symbol for eggs, or a part of an address Like data instructions. also called OPERATIONS. come in several flavors. Each class of data comes with a set of PRIMITIVE OPERATIONS. For numbers, we naturally get +, -,* and so on. Programmers words of a foreign language and of programming as forming sentences in this languageas the compose primitive operations into PROGRAMS. Thus, we may think of primitive operations TEAM FLY PRESENTS
-19- Section 1 Students, Teachers, and Computers We learn to compute at a young age. At first we just add and subtract numbers. One plus one equals two. Five minus two is three. As we grow older we learn about additional mathematical operations, like exponentiation and sine, but we also learn to describe rules of computation. Given a circle of radius r, its circumference is r times two times pi. A minimum-wage laborer who works for N hours earns N times 5.35 dollars. The truth is, our teachers turn us into computers and program us to execute simple computer programs. So, the secret is out. Computer programs are just very fast students. They can perform millions of additions while we might still be struggling with the first one. But computer programs can do more than just manipulate numbers. They can guide an airplane. They can play games. They can look up a person's phone number. They can print the payroll checks for huge corporations. In short, computers process all kinds of information. People state information and instructions in English. The temperature is 35o C; convert this temperature into Fahrenheit. It takes this car 35 seconds to accelerate from zero to 100 miles per hour; determine how far the car gets in 20 seconds. Computers, however, barely understand basic English and certainly can't understand complex instructions expressed in English. Instead we must learn to speak a computer language so that we can communicate information and instructions. A computer's language of instruction and information is a PROGRAMMING LANGUAGE. Information expressed in a programming language is called DATA. There are many flavors of data. Numbers are one class of data. Number series belong to the class of COMPOUND DATA, because each series is made up of other pieces of smaller pieces of data, namely, numbers. To contrast the two kinds of data, we also call numbers ATOMIC DATA. Letters are other examples of atomic data; family trees are compound data. Data represents information, but the concrete interpretation is up to us. For example, a number like 37.51 may represent a temperature, a time, or a distance. A letter like ``A'' may denote a school grade, a quality symbol for eggs, or a part of an address. Like data, instructions, also called OPERATIONS, come in several flavors. Each class of data comes with a set of PRIMITIVE OPERATIONS. For numbers, we naturally get +, -, *, and so on. Programmers compose primitive operations into PROGRAMS. Thus, we may think of primitive operations as the words of a foreign language and of programming as forming sentences in this language. TEAMFLY TEAM FLY PRESENTS
Some programs are as small as essays. Others are like sets of encyclopedias. Writing good essays and books requires careful planning, and writing good programs does, too Small or large, a good program 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, but it is not easy. The best part of being a programmer is watching our products"grow and become successful. It is fun to observe a computer program play a game. It is exciting to see a computer program help someone. To get to this point, however, we must practice many skills. As we will find out, programming languages are primitive; especially, their grammar is restrictive. And unfortunately, computers are stupid The smallest grammatical mistake in a program is a fatal stumbling block for a computer. Worse, once our program is in proper grammatical shape, it might not perform the computations intended Programming a computer requires patience and concentration. Only attention to minute details will avoid frustrating grammatical mistakes. Only rigorous planning and adherence to the plan will prevent serious logical mistakes in our designs. But when we finally the design of programs, we will have learned skills that are useful far beyond the realm of programming L 20- TEAM FLY PRESENTS
-20- Some programs are as small as essays. Others are like sets of encyclopedias. Writing good essays and books requires careful planning, and writing good programs does, too. Small or large, a good program 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, but it is not easy. The best part of being a programmer is watching our ``products'' grow and become successful. It is fun to observe a computer program play a game. It is exciting to see a computer program help someone. To get to this point, however, we must practice many skills. As we will find out, programming languages are primitive; especially, their grammar is restrictive. And unfortunately, computers are stupid. The smallest grammatical mistake in a program is a fatal stumbling block for a computer. Worse, once our program is in proper grammatical shape, it might not perform the computations as intended. Programming a computer requires patience and concentration. Only attention to minute details will avoid frustrating grammatical mistakes. Only rigorous planning and adherence to the plan will prevent serious logical mistakes in our designs. But when we finally master the design of programs, we will have learned skills that are useful far beyond the realm of programming. Let's get started! TEAMFLY TEAM FLY PRESENTS