CONTENTS
Preface iii
Dedication v

Chapter 1 The Robot World 1
1.1 The Robot World 1
1.2 Robot Capabilities 3
1.3 Tasks and Situations 4
1.4 Robots and Objects 5
1.5 Important Ideas From This Chapter 5
1.6 Problem Set 6
Chapter 2 Primitive Instructions and Simple Programs 8
2.1 Changing Position 8
2.2 Turning in Place 9
2.3 Finishing a Task 10
2.4 Handling Beepers 10
2.5 Robot Descriptions 11
2.6 A Complete Program 13
   2.6.1 Executing a Program 14
   2.6.2 The Form of Robot Programs 16
2.7 Error Shutoffs 18
2.8 Programming Errors 18
   2.8.1 Bugs and Debugging 21
2.9 A Task for Two Robots 21
2.10 An infinity of Beepers 22
2.11 Some Terminology 23
2.12 Important Ideas From This Chapter 23
2.13 Problem Set 23
Chapter 3 Extending the Robot Programming Language 29
3.1 Creating a More Natural Programming Language 29
3.2 A Mechanism that Defines New Classes of Robots 30
3.3 Defining the New Methods 31
3.4 The Meaning and Correctness of New Methods 34
3.5 Defining New Methods in a Program 35
3.6 Modifying Inherited Methods 38
3.7 An Ungrammatical Program 39
3.8 Tools for Designing and Writing Robot Programs 41
   3.8.1 Stepwise Refinement-a Technique for Planning, Implementing, and Analyzing Robot Programs 41
   3.8.2 The Second Step-Planning harvestTwoRowsand positionForNextHarvest 45
   3.8.3 The Third Step-Planning harvestOneRow and goToNextRow 47
   3.8.4 The Final Step-Verifying That the Complete Program is Correct 48
3.9 Advantages of Using New Instructions 50
   3.9.1 Avoiding Errors 51
   3.9.2 Future Modifications 52
   3.9.3 A Program Without New instructions 53
3.10 Mixin Inheritance 55
3.11 Writing Understandable Programs 57
3.12 Important Ideas From This Chapter 58
3.13 Problem Set 59
Chapter 4 Polymorphism 64
4.1 Robot Teams 64
   4.1.1 Team Basics 64
   4.1.2 Organizers 66
4.2 Similar Tasks 68
4.3 Object Oriented Design -- Clients and Servers 71
4.4 Choreographers 79
   4.4.1 The Devil is in the Details 79
   4.4.1 How it Fits Together 82
4.5 Using Polymorphism 84
4.6 Still More on Polymorphism -- Strategy and Delegation 88
4.7 Python List and More on Strategies 92
4.8 Decorators 95
4.9 Observers 98
   4.9.1 The Simple Case of a Single Observer 98
   4.9.2 Multiple Observers 100
4.10 Final Words on Polymorphism 101
4.11 Important Ideas From This Chapter 102
4.12 Problem Set 103
Chapter 5 Conditionally Executing Instructions 107
5.1 The IF Instruction 107
5.2 The Conditions That Robots Can Test 108
   5.2.1 Writing New Predicates 111
5.3 Simple Examples of the IF Instruction 112
   5.3.1 The harvestOneRowMethod 113
   5.3.2 The faceNorthIfFacingSouth Method 114
   5.3.3 The faceNorthMethod 115
   5.3.4 Determining the correctness of the IF Instruction 117
5.4 The IF/ELSE Instruction 118
5.5 Nested IF Instructions 119
5.6 More Complex Tests 123
5.7 The IF/ELIF and IF/ELIF/ELSE Instructions 125
5.8 When to Use an IF Instruction 127
5.9 Transformations for Simplifying IF Instructions 129
5.10 Polymorphism Revisited 132
5.11 Important Ideas From This Chapter 134
5.12 Problem Set 134
Chapter 6 Instructions That Repeat 139
6.1 The FOR-LOOP Instruction 139
6.2 The WHILE Instruction 141
   6.2.1 Why WHILE is Needed 141
   6.2.2 The Form of the WHILE Instruction 142
   6.2.3 Building a WHILE Loop - the Four Step Process 143
   6.2.4 A More Interesting Problem 144
6.3 Errors to Avoid with WHILE Loops 146
   6.3.1 The Fence Post Problem 147
   6.3.2 Infinite Execution 148
   6.3.3 When the test of a WHILE is Checked 149
6.4 Nested WHILE Loops 150
   6.4.1 A Good Example of Nesting 150
   6.4.2 A Bad Example of Nesting 153
6.5 WHILE and IF Instructions 158
6.6 Reasoning about Loops 159
6.7 A Large Program Written by Stepwise Refinement 162
6.8 When to Use a Repeating Instruction 167
6.9 Important Ideas From This Chapter 169
6.10 Problem Set 169
Chapter 7 Advanced Techniques for Robots 182
7.1 Introduction to Recursion 182
7.2 More on Recursion 184
7.3 Tail Recursion and Looping 187
7.4 Going Formal 188
7.5 Searching 189
7.6 Doing Arithmetic 193
7.7 Polymorphism--Why Write Many Programs When One Will Do? 197
7.8 Dynamic Python 199
7.9 Conclusion 201
7.10 Important Ideas From This Chapter 202
7.11 Problem Set 202
Chapter 8 Concurrent Robot Programs 207
8.1 Simple Concurrent Programs 207
8.2 Robot Runs In Its Own Thread 208
8.3 Cooperation 209
8.4 Race Conditions 210
8.5 Deadlock 210
8.6 Important Ideas From This Chapter 213
8.7 Problem Set 213
Appendix 215
1 Python __main__ 215
2 KarelRunner and Typical imports 215
3 Executing Robot Code in Python 217
4 Constructors in Python 218
5 Python Cloning 220
6 Karel's World (RobotWorld) 221
7 Common Python Errors 222
8 Python Tools 222
9 Public and Private in Python 223
Index 224