Caml quick reference guide

Contact the author Pierre.Weis@inria.fr
Created July 1996.

(* This is a comment in Caml *)

Constants

Integers: 1 + 2 * 3 - 1 = 6
Operations: x mod y, x / y, x land y, x lor y, x lxor y, lnot x, x lsl y, x lsr y, x asr y
int_of_string, string_of_int, int_of_float

Example

Float: 1.0 +. 2.0 *. 3.0 -. 1.0 = 6.0
Operations: sin, cos, exp, sqrt, atan, atan2
float_of_string, string_of_float, float_of_int

Example

Strings: "Hello\n"
Operations: Concatenation ^, make_string, string_length, sub_string, s.[0], s.[0] <- `a`

Example

Characters: `a`
Operations: int_of_char, char_of_int

Booleans: true, false
Operations: &&, ||, not

Example

Nothing: type unit, value (), argument or result with no particular meaning.

Example

Using parentheses

In Caml, parens are significant and use the mathematical rules of trigonometric functions.

In Caml, 1 + 2 * x means 1 + (2 * x)
as in math.

In Caml, f x means f (x)
as in math sin x means sin (x).

In Caml, f x + g x means (f x) + (g x)
as in math sin x + cos x means (sin x) + (cos x).
Hence, f x - y means (f x) - y
as in math sin x - 1 means (sin x) - 1.

In Caml, this rule is generalized to any infix operator:
f x :: g x means (f x) :: (g x).

Defining Values

Global : let ident = expression;;
Local : let ident = expression in ...
Multiple : let ... and ident2 = expression2 and ident3 = ...
Recursive : let rec x = ...

Defining Functions

A single argument: let f x = expression
Several arguments: let f x y = expression
With matching: let f = function matching
Recursive : let rec f x = ...

Special cases: a procedure is a function with no mathematically sound result, it returns ().
For instance, assignment functions return ().

Calling Functions

Using parens: f (x)
Using a space: f x
More than one arguments: f (x) (y) or f x y
Warning: if an argument is complex you must enclose it between parens.
For instance f (x + 1) (and not f x + 1, which means f (x) + 1, that is 1 + f (x)).

Special cases: functions with no argument get () as argument.
For instance print_newline is called with print_newline ().

Matching

A pattern matching appears after the keywords function, or with
(in function matching, try ... with matching, or match ... with matching).
Warning: a pattern matching which is nested inside another one must be enclosed by begin end keywords.

A pattern matching is a list of clauses pattern -> expression: each clause is tried in the order of presentation; the first clause for which the pattern corresponds to (is more general than) the value is selected; the corresponding expression is then returned.

There are constant patterns, variable patterns or compound patterns.

 | constant_pattern -> expression
 | variable_pattern -> expression
 | compound_pattern -> expression;;
Constant pattern: the constants of the language, such as 1, "poi", true, or constants constructors.
For instance
 let rec fact = function
 | 0 -> 1
 | n -> n * fact (n - 1);;

Variable pattern: identifiers or the character _
For instance

 let rec fib = function
 | 0 -> 1
 | 1 -> 1
 | n -> fib (n - 1) + fib (n - 2);;

Compound pattern: a constructor applied to a pattern Constructor pattern, lists x :: l, tuples (pattern1, pattern2), records {label1=pattern1; ...; labeln=patternn}.

Complex patterns:

Explicit call to the pattern matching

match expression with matching

For instance:

 match f 2 with
 | 1 -> expression
 | n -> expression

Anonymous function

With a single argument: function x -> expression

With pattern matching on this argument:

 function
 | pattern -> expression
 | ...
 | pattern -> expression
With several arguments: function x -> function y -> expression

References

Definition: let x = ref 0 in ...

Access: operator ! (! reference)

Assignment: operator := (reference := value)
For instance: x := !x + 1

Vectors


Definition: [| 1; 2; 3 |] or make_vect number_of_elements initial_value
Access: v.(0)
Assignment: v.(0) <- new_value
Iteration: do_vect, map_vect
for i = 0 to vect_length v - 1 do ... done
Functions: vect_length, , blit_vect

Lists


Definition: [], [ 1; 2; 3 ], or x :: l
Access:
 match l with
 | [] -> ...
 | x :: l -> ...
Assignment: a list is immutable.
Iteration: do_list, map
Functions: list_length

Loops

for i = 0 to 10 do print_int i done
for i = 10 downto 0 do print_int i done
while !j > 0 do j := !j - 1 done

Sequence

Syntax: expression; expression
Warning: must be enclosed by begin end in the arms of a conditional if then else.

Conditional

Syntax: if condition then expression else expression
Warning: if condition then begin e1; e2 end else begin e3; e4 end

Standard comparison operators: <, >, <=, >=, <>
Physical equality: ==, !=

Exceptions

Handling: try expression with matching
Throwing: raise constant_exception or raise (exception_with_argument expression)

Input-output

Defining Types

Sum type:
 type name =
 | Constant_constructor
 | Constructor_with_argument of expression_de_type;;

Record type:

 type name = {label1 : type_of_the_field; label2 :
type_of_the_field};;
A field may be mutable, if declared as: mutable label : type_of_the_mutable_field
Assignment: record.label <- new_value

Type abbreviation:

 type name == type_expression;;

Defining exceptions

exception Constant_Exception;;
exception Exception_with_argument of type_expression;;


Caml home page Last modified: Thursday, December 5, 2002
Copyright © 1995 - 2004, INRIA all rights reserved.

Contact the author Pierre.Weis@inria.fr