Download Introduction to the Objective Caml Programming Language

Transcript
Contents
1 Introduction
9
1.1
Functional and imperative languages . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
1.2
Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
1.3
Additional Sources of Information . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
2 Simple Expressions
13
2.1
Comment convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
2.2
Basic expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
2.2.1
unit: the singleton type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
2.2.2
int: the integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
2.2.3
float: the floating-point numbers . . . . . . . . . . . . . . . . . . . . . . . .
16
2.2.4
char: the characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
2.2.5
string: character strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18
2.2.6
bool: the Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
2.3
Operator precedences
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
2.4
The OCaml type system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
2.5
Compiling your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
3 Variables and Functions
3.1
25
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
26
3.1.1
Scoping and nested functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
3.1.2
Recursive functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
3