The Whole Book
Total Page:16
File Type:pdf, Size:1020Kb
Picturing Programs An introduction to computer programming Stephen Bloch1 1Math/CS Department, Adelphi University. Supported in part by NSF grant 0618543. Any opinions, findings and conclusions or recommendations expressed in this material are those of the author and do not necessarily reflect the views of the National Science Foundation (NSF). Dedicated to my wife Deborah, with whom I would have done more fun things in the past year if I hadn’t been busy writing a book. vi Contents 0 Introduction 1 0.1 Languagesanddialects. 1 0.2 Problems,programs,andprogramtesting . ..... 2 0.3 UsingDrRacket................................. 3 0.3.1 GettingDrRacket ........................... 3 0.3.2 StartingDrRacket ........................... 3 0.3.3 Choosinglanguages .......................... 4 0.3.4 Installinglibraries . 4 0.3.5 Gettinghelp .............................. 5 0.4 Textbookwebsite ............................... 5 PARTIRunningandwritingprograms 7 1 Drawing pictures 9 1.1 Workingwithpictures ............................. 9 1.1.1 ImportingpicturesintoDrRacket. 9 1.1.2 The Interactions and Definitions panes . ... 9 1.1.3 Choosinglibraries ........................... 10 1.2 Manipulatingpictures . 10 1.2.1 Terminology .............................. 11 1.2.2 Combiningpictures........................... 11 1.2.3 ASyntaxRule,Sorta ......................... 12 1.3 Makingmistakes ................................ 13 1.3.1 Leaving out the beginning left-parenthesis . ...... 13 1.3.2 Leaving out the ending right-parenthesis . ..... 14 1.3.3 Misspellingtheoperationname . 15 1.3.4 Toofewortoomanyarguments . 15 1.3.5 Putting the operation and arguments in the wrong order ...... 15 1.3.6 Doing something different from what you meant . 16 1.4 GettingHelp .................................. 16 1.5 Morecomplexmanipulations . 16 1.6 SavingYourWork: theDefinitionspane . 18 1.7 TheStepper................................... 19 1.8 Syntaxandboxdiagrams ........................... 20 1.9 Review...................................... 23 1.10Reference .................................... 24 vii viii CONTENTS 2 Variables 25 2.1 Definingavariable ............................... 25 2.2 TheDefinitionspane.............................. 27 2.3 What’sinaname? ............................... 27 2.4 Moresyntaxrules................................ 28 2.5 VariablesandtheStepper . 29 2.6 Review...................................... 29 2.7 Reference .................................... 29 3 Building more interesting pictures 31 3.1 Otherkindsofarguments . 31 3.1.1 Stringsasarguments.......................... 31 3.1.2 Numbersasarguments. 33 3.2 Moremistakes ................................. 34 3.3 Creatingsimpleshapes. 34 3.4 Datatypesandcontracts . 36 3.4.1 Stringliteralsandidentifiers . 36 3.4.2 Functioncontracts ........................... 38 3.4.3 Comments................................ 39 3.4.4 CommentsinPractice . 40 3.5 Morefunctionsonpictures. 41 3.5.1 Cuttinguppictures .......................... 41 3.5.2 Measuringpictures........................... 42 3.5.3 Placingimagesprecisely . 43 3.5.4 Text................................... 44 3.5.5 Forfurtherreading.... 45 3.5.6 Playingwithcolors........................... 45 3.6 Specifying results and checking your work . ...... 46 3.7 Readingandwritingimages . 47 3.8 Expandingthesyntaxrules . 48 3.9 Review...................................... 48 3.10Reference .................................... 49 4 Writing functions 51 4.1 Definingyourownfunctions. 51 4.2 What’sinadefinition?............................. 53 4.2.1 Terminology .............................. 53 4.2.2 Linesandwhitespace ......................... 53 4.3 Parametersandarguments . 55 4.4 Parameters,arguments,andtheStepper . ..... 56 4.5 TestingaFunctionDefinition . 58 4.5.1 Testingwithstringdescriptions . 58 4.5.2 Commonbeginnermistakes . 60 4.5.3 The check-expect function...................... 61 4.6 Anewsyntaxrule ............................... 62 4.7 Scopeandvisibility.............................. 64 4.8 AnanalogyfromEnglish. 65 4.8.1 Propernounsandliterals . 65 4.8.2 Pronounsandvariables . 65 4.8.3 Impropernounsanddatatypes. 66 CONTENTS ix 4.8.4 Verbsandfunctions .......................... 66 4.8.5 Nounphrasesandexpressions. 66 4.9 Review...................................... 67 4.10Reference .................................... 68 5 A recipe for defining functions 69 5.1 Step-by-steprecipes ............................. 69 5.2 Amoredetailedrecipe............................. 69 5.3 Function contracts and purpose statements . ...... 70 5.4 Examples(alsoknownasTestCases). 73 5.5 Thefunctionskeleton ............................. 75 5.6 Commonbeginnermistakes . 75 5.7 Checkingsyntax ................................ 78 5.8 Exercisesonwritingskeletons . 79 5.9 Theinventory.................................. 79 5.10 Inventorieswithvalues. 82 5.11Thefunctionbody ............................... 83 5.12Testing ..................................... 86 5.13 Usingthefunction ............................... 86 5.14 Puttingitalltogether . 87 5.15Review...................................... 88 5.16Reference .................................... 89 6 Animations in DrRacket 91 6.1 Preliminaries .................................. 91 6.2 Tickhandlers .................................. 92 6.3 Commonbeginnermistakes . 93 6.4 Writingtickhandlers. 95 6.5 Writingdrawhandlers ............................. 97 6.6 Otherkindsofeventhandlers . 99 6.7 Designrecipe .................................. 105 6.8 Anoteonsyntax................................ 106 6.9 Recording .................................... 107 6.10Review...................................... 107 6.11Reference .................................... 108 7 Working with numbers 109 7.1 Arithmeticsyntax ............................... 109 7.2 Variablesandnumbers. 110 7.3 Prefixnotation ................................. 112 7.4 Arecipeforconvertingfrominfixtoprefix. 113 7.5 Kindsofnumbers................................ 115 7.5.1 Integers ................................. 115 7.5.2 Fractions ................................ 115 7.5.3 Inexactnumbers ............................ 116 7.6 Contracts for built-in arithmetic functions . ......... 116 7.7 Writingnumericfunctions . 117 7.8 Manipulatingcolorsinimages. 125 7.8.1 Images,pixels,andcolors . 125 7.8.2 Buildingimagespixelbypixel. 125 x CONTENTS 7.8.3 Error-proofing ............................. 127 7.8.4 Buildingimagesfromotherimages . 129 7.8.5 Asneakpreview ............................ 131 7.8.6 Aproblemwithbit-maps . 131 7.9 Randomness................................... 131 7.9.1 Testingrandomfunctions . 132 7.9.2 Exercisesonrandomness. 133 7.10Review...................................... 134 7.11Reference .................................... 134 8 Animations involving numbers 137 8.1 Modelandview................................. 137 8.2 Designrecipe .................................. 139 8.3 Animations using add1 ............................. 141 8.4 Animationswithothernumericfunctions . 143 8.5 Randomnessinanimations. 146 8.6 Review...................................... 147 8.7 Reference .................................... 147 9 Working with strings 149 9.1 Operations ................................... 149 9.2 Stringvariablesandfunctions . 150 9.3 Review...................................... 151 9.4 Reference .................................... 151 10 Animations with arbitrary models 153 10.1Modelandview................................. 153 10.2Designrecipe .................................. 153 10.3Review...................................... 156 10.4Reference .................................... 156 11 Reduce, re-use, recycle 157 11.1 Planning for modification and extension . 157 11.2 Re-usingvariables .............................. 157 11.3 Composingfunctions . 161 11.4 Designingforre-use ............................. 163 11.5 Designing multi-function programs: a case study . ......... 164 11.6 Practicalities of multi-function programs . .......... 175 11.7 Re-usingdefinitionsfromotherfiles. 176 11.7.1 require and provide ......................... 177 11.7.2 provide-ingeverything ........................ 178 11.8Review...................................... 178 11.9Reference .................................... 179 PARTIIDefinitionbyChoices 181 12 Defining types 183 CONTENTS xi 13 Booleans 185 13.1Anewdatatype ................................ 185 13.2 Comparingstrings ............................... 185 13.3 Comparingnumbers .............................. 187 13.4 Designing functions involving booleans . ........ 190 13.5Comparingimages ............................... 191 13.6Testingtypes .................................. 191 13.7 Booleanoperators ............................... 192 13.8 Short-circuitevaluation . 196 13.9Review...................................... 196 13.10Reference .................................... 197 14 Animations with Booleans 199 14.1 Stoppinganimations . 199 14.2 Stoppinginresponsetoevents . 202 14.3Review...................................... 203 14.4Reference .................................... 205 15 Conditionals 207 15.1 Makingdecisions ................................ 207 15.2 Elseanderror-handling . 210 15.3Designrecipe .................................. 211 15.4 Casestudy:bankinterest . 214 15.5 Orderingcasesinaconditional . 217 15.6 Unnecessaryconditionals. 219 15.7 Nestedconditionals. 221 15.8 Decisionsamongdatatypes . 225 15.9Review...................................... 228 15.10Reference .................................... 229 16 New