An expression is the description of a computation. Evaluation of an expression returns a value at the end of the computation. The execution of an Objective CAML program corresponds to the computation of an expression. Functions, program execution control structures, even conditions or loops, are themselves also expressions.

Static typing guarantees that the computation of an expression cannot cause a run-time type error. In fact, application of a function to some arguments (or actual parameters) isn't accepted unless they all have types compatible with the formal parameters indicated in the definition of the function. Furthermore, the Objective CAML language has type infererence: the compiler automatically determines the most general type of an expression.

Finally a minimal knowledge of the representation of data is indispensable to the programmer in order to master the effects of physical modifications to the data.

Chapter 3 exhibits the imperative style. The constructions there are closer to classic languages. Associative control structures such as sequence and iteration are presented there, as well as mutable data structures. The interaction between physical modifications and sharing of data is then detailed. Type inference is described there in the context of these new constructions.

Chapter 4 compares the two preceding styles and especially presents different mixed styles. This mixture supports in particular the construction of lazy data structures, including mutable ones.

Chapter 5 demonstrates the use of the

These first four chapters are illustrated by a complete example, the implementation

of a calculator, which evolves from chapter to chapter.

Chapter 6 presents three complete applications: a little database, a mini-BASIC interpreter and the game Minesweeper. The first two examples are constructed mainly in a functional style, while the third is done in an imperative style.

value declaration | : | let |

exception declaration | : | exception |

type declaration | : | type |

Here's a first (little) Objective CAML program, to be entered into the toplevel, whose prompt is the pound character (

`#`

),
in which a function fact
computing the factorial of a natural number, and
its application to a natural number `8`

, are defined.
This program consists of two

#let

rec`fact`

`n`

`=`

if`n`

`<`

`2`

then

`1`

else`n`

`*`

`fact`

(n`-`

`1`

)`;;`

`val fact : int -> int = <fun>`

# fact

`8`

`;;`

`- : int = 40320`

`-`

) in the case of an expression; the inferred
type; and the return value. In the case of a function value, the system
prints `<fun>`

.The following example demonstrates the manipulation of functions as values in the language. There we first of all define the function succ which calculates the successor of an integer, then the function compose which composes two functions. The latter will be applied to fact and succ.

This last call carries out the computation fact

#let`succ`

`x`

`=`

`x`

`+`

`1`

`;;`

`val succ : int -> int = <fun>`

#let`compose`

`f`

`g`

`x`

`=`

`f`

(g`x`

)

`;;`

`val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun>`

# compose`fact`

`succ`

`8`

`;;`

`- : int = 362880`

` `

`8`

`8`

.