Contents
Preface i1 Introduction 1
1.1 Functional and imperative languages . . . . . . . . . . . . . . . . . . 31.2 Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.3 Additional Sources of Information . . . . . . . . . . . . . . . . . . . 3
2 Simple Expressions 5
2.1 Comment convention . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Basic expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2.1
unit
: the singleton type . . . . . . . . . . . . . . . . . . . . 62.2.2
int
: the integers . . . . . . . . . . . . . . . . . . . . . . . . 62.2.3
float
: the floating-point numbers . . . . . . . . . . . . . . . 72.2.4
char
: the characters . . . . . . . . . . . . . . . . . . . . . . 82.2.5
string
: character strings . . . . . . . . . . . . . . . . . . . . 82.2.6
bool
: the Boolean values . . . . . . . . . . . . . . . . . . . . 92.3 Operator precedences . . . . . . . . . . . . . . . . . . . . . . . . . . 102.4 The OCaml type system . . . . . . . . . . . . . . . . . . . . . . . . . 112.5 Compiling your code . . . . . . . . . . . . . . . . . . . . . . . . . . 122.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3 Variables and Functions 15
3.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.1.1 Scoping and nested functions . . . . . . . . . . . . . . . . . . 183.1.2 Recursive functions . . . . . . . . . . . . . . . . . . . . . . . 183.1.3 Higher order functions . . . . . . . . . . . . . . . . . . . . . 193.2 Variable names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.3 Labeled parameters and arguments . . . . . . . . . . . . . . . . . . . 213.3.1 Rules of thumb . . . . . . . . . . . . . . . . . . . . . . . . . 223.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4 Basic pattern matching 29
4.1 Functions with matching . . . . . . . . . . . . . . . . . . . . . . . . 304.2 Pattern expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Leave a Comment