Table of Contents
Preface xiii
1 The Way of the Program 1
What Is a Program? 1
Running Julia 2
The First Program 3
Arithmetic Operators 3
Values and Types 4
Formal and Natural Languages 5
Debugging 6
Glossary 7
Exercises 9
2 Variables, Expressions, and Statements 11
Assignment Statements 11
Variable Names 12
Expressions and Statements 12
Script Mode 13
Operator Precedence 14
String Operations 15
Comments 16
Debugging 16
Glossary 17
Exercises 19
3 Functions 21
Function Calls 21
Math Functions 22
Composition 23
Adding New Functions 23
Definitions and Uses 24
Flow of Execution 25
Parameters and Arguments 26
Variables and Parameters Are Local 27
Stack Diagrams 27
Fruitful Functions and Void Functions 28
Why Functions? 29
Debugging 30
Glossary 30
Exercises 32
4 Case Study: Interface Design 35
Turtles 35
Simple Repetition 36
Exercises 37
Encapsulation 39
Generalization 39
Interface Design 40
Refactoring 41
A Development Plan 42
Docstring 43
Debugging 44
Glossary 44
Exercises 45
5 Conditionals and Recursion 49
Floor Division and Modulus 49
Boolean Expressions 50
Logical Operators 51
Conditional Execution 51
Alternative Execution 51
Chained Conditionals 52
Nested Conditionals 52
Recursion 53
Stack Diagrams for Recursive Functions 55
Infinite Recursion 55
Keyboard Input 56
Debugging 57
Glossary 58
Exercises 59
6 Fruitful Functions 63
Return Values 63
Incremental Development 65
Composition 67
Boolean Functions 67
More Recursion 68
Leap of Faith 71
One More Example 71
Checking Types 72
Debugging 73
Glossary 74
Exercises 74
7 Iteration 77
Reassignment 77
Updating Variables 78
The while Statement 79
Break 80
Continue 81
Square Roots 82
Algorithms 83
Debugging 84
Glossary 84
Exercises 85
8 Strings 87
Characters 87
A String Is a Sequence 88
Length 88
Traversal 89
String Slices 90
Strings Are Immutable 91
String Interpolation 91
Searching 92
Looping and Counting 93
String Library 93
Thee ∈ Operator 94
String Comparison 94
Debugging 95
Glossary 96
Exercises 97
9 Case Study: Word Play 101
Reading Word Lists 101
Exercises 102
Search 103
Looping with Indices 104
Debugging 106
Glossary 107
Exercises 107
10 Arrays 109
An Array Is a Sequence 109
Arrays Are Mutable 110
Traversing an Array 111
Array Slices 112
Array Library 112
Map, Filter, and Reduce 113
Dot Syntax 115
Deleting (Inserting) Elements 115
Arrays and Strings 116
Objects and Values 117
Aliasing 118
Array Arguments 119
Debugging 121
Glossary 122
Exercises 124
11 Dictionaries 127
A Dictionary Is a Mapping 127
Dictionaries as Collections of Counters 129
Looping and Dictionaries 130
Reverse Lookup 131
Dictionaries and Arrays 132
Memos 133
Global Variables 135
Debugging 137
Glossary 138
Exercises 139
12 Tuples 141
Tuples Are Immutable 141
Tuple Assignment 142
Tuples as Return Values 143
Variable-Length Argument Tuples 144
Arrays and Tuples 145
Dictionaries and Tuples 146
Sequences of Sequences 148
Debugging 149
Glossary 149
Exercises 150
13 Case Study: Data Structure Selection 153
Word Frequency Analysis 153
Random Numbers 154
Word Histogram 155
Most Common Words 156
Optional Parameters 157
Dictionary Subtraction 158
Random Words 159
Markov Analysis 159
Data Structures 161
Debugging 163
Glossary 164
Exercises 165
14 Files 167
Persistence 167
Reading and Writing 168
Formatting 168
Filenames and Paths 169
Catching Exceptions 170
Databases 171
Serialization 172
Command Objects 173
Modules 174
Debugging 175
Glossary 176
Exercises 177
15 Structs and Objects 179
Composite Types 179
Structs Are Immutable 180
Mutable Structs 181
Rectangles 181
Instances as Arguments 182
Instances as Return Values 184
Copying 184
Debugging 184
Glossary 185
Exercises 186
16 Structs and Functions 189
Time 189
Pure Functions 190
Modifiers 191
Prototyping Versus Planning 192
Debugging 194
Glossary 195
Exercises 195
17 Multiple Dispatch 197
Type Declarations 197
Methods 198
Additional Examples 199
Constructors 200
Show 201
Operator Overloading 202
Multiple Dispatch 203
Generic Programming 204
Interface and Implementation 205
Debugging 205
Glossary 206
Exercises 207
18 Subtyping 209
Cards 209
Global Variables 210
Comparing Cards 211
Unit Testing 211
Decks 212
Add, Remove, Shuffle, and Sort 213
Abstract Types and Subtyping 213
Abstract Types and Functions 215
Type Diagrams 216
Debugging 217
Data Encapsulation 218
Glossary 219
Exercises 220
19 The Goodies: Syntax 223
Named Tuples 223
Functions 224
Blocks 225
Control Flow 227
Types 228
Methods 230
Constructors 230
Conversion and Promotion 231
Metaprogramming 232
Missing Values 234
Calling C and Fortran Code 234
Glossary 235
20 The Goodies: Base and Standard Library 237
Measuring Performance 237
Collections and Data Structures 238
Mathematics 240
Strings 240
Arrays 241
Interfaces 243
Interactive Utilities 244
Debugging 245
Glossary 246
21 Debugging 247
Syntax Errors 247
Runtime Errors 249
Semantic Errors 253
A Unicode Input 257
B JuliaBox 259
Index 263