Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Lecture notes · chapter 2

Caml basics

Definitions, basic types, expressions and the first predefined functions.

Caml in general

Principles

Introducing Caml

Caml is a simple language:

  • few instructions;
  • general constructs;
  • intuitive notations.

Here we use it through its interpreter (the “toplevel”, started with the command ocaml). You type an expression, ended by ;;. Caml reads it, computes its type, evaluates it, then prints the type and the value.

In this course, the interpreter runs in the page: each code frame is a cell that it executes, and its answer is shown under each phrase. Edit the code, then click Run to run it again. The cells of a page share their definitions, from top to bottom.

1 + 2 ;;
"Caml" ;;

In the answer, the - means that the value has no name. In the ocaml interpreter, on your own machine, you type the same phrases after the # prompt, and the answers are the same.

If the expression is ill-typed, Caml refuses to evaluate it:

1 + "deux" ;;

All values are typed! And the type is checked before execution: a type error cannot happen while the program is running.

Definitions

How do we reuse a value in Caml? With definitions. The keyword to use is let.

Global definition: the name can be used in everything that follows.

let x = 3 * 4 ;;
x + 1 ;;

Local definition: let … in … gives a name for the duration of a single expression.

let y = 2 in x + y ;;
y ;;

Multiple definitions: several names at once, joined by and.

let a = 1 and b = 2 ;;

Caml types

Numbers

There are two number formats:

  1. integers (int): -, +, *, / (integer division) and mod (remainder); from min_int to max_int;
  2. floats (float), mind the .: -., +., *. and /..
7 / 2 ;;
7 mod 2 ;;
max_int ;;
max_int + 1 ;;
1.5 +. 2.25 ;;

Integer arithmetic “wraps around”: beyond max_int, it starts again from min_int, without any error.

Float operators are different from integer ones, and Caml never converts on its own:

1 + 2.5 ;;
1.0 + 2.0 ;;
float_of_int 3 +. 0.5 ;;

Boolean values

The type bool stands for truth values, true and false.

abnot aa || ba && b
falsefalsetruefalsefalse
falsetruetruetruefalse
truefalsefalsetruefalse
truetruefalsetruetrue
not true ;;
true && false ;;
1 < 2 || 1 / 0 = 0 ;;
1 > 2 && 1 / 0 = 0 ;;

In both cases, 1 / 0 is never computed: the answer is known from the left-hand part.

Characters and strings

  • 'a' is a character (type char), represented by an 8-bit integer (its code).
  • "bonjour" is a character string (type string).
  • Operators: ^ for concatenation, .[i] for the character at index i (counting from 0).
'a' ;;
"bon" ^ "jour" ;;
"bonjour".[3] ;;
String.length "bonjour" ;;
int_of_char 'A' ;;

Comparison operators

These are the operators that test:

  • equality: =;
  • inequality: <>;
  • order relations: >, <, <= and >=.

Special features:

  • these operators are the only ones that can be used on several types: they are polymorphic;
  • the two values compared must however be of the same type;
  • they have higher precedence than the logical operators: no parentheses are needed (but they are strongly recommended).
1 < 2 ;;
"abc" < "abd" ;;
'a' = 'b' ;;
1 <> 2 ;;
1 = 1.0 ;;

Strings are compared in dictionary order (more precisely, in the order of the character codes).