`(* This is a comment in Caml *)`

`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`

**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`

**Strings**: `"Hello\n"`

Operations: Concatenation `^`

, `make_string`

, `string_length`

, `sub_string`

, `s.[0]`

,
`s.[0] <- `a``

**Characters**: ``a``

Operations:
`int_of_char, char_of_int`

**Booleans**: `true, false`

Operations:
`&&`

, `||`

, `not`

**Nothing**: type `unit`

, value `()`

,
argument or result with no particular meaning.

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)**.

**let** ident = expression;;

Local :

`let ident = expression `**in**

...
Multiple :

`let ... `**and** ident2 = expression2
**and** ident3 =

...
Recursive :

`let `**rec** x = ...

`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 `()`

.

`f (x)`

Using a space:

`f x`

More than one arguments:

`f (x) (y)`

or `f x y`

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 ()`

.

`function`

, or
`with`

(in

`function`

`try ... with`

`match ... with`

`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:

- tuple pattern:
`| (pattern, pattern) -> expression`

- alias pattern:
`| pattern`

**as**ident -> expression - or pattern:
`| constant_pattern`

**|**constant_pattern -> expression - range pattern:
`| character pattern`

**..**character pattern -> expression

Example:let letter = function | `a` .. `z` | `A` .. `Z` -> true | _ -> false;;

- Clause with guard:
`| pattern`

**when**condition -> expression

Example:let letter = function | c when c >= `a` && c <= `z` || c >= `A` && c <= `Z` -> true | _ -> false;;

**match** expression **with** matching

For instance:

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

**function** x
**->** expression

With pattern matching on this argument:

function | pattern -> expression | ... | pattern -> expressionWith several arguments:

```
function x -> function y ->
expression
```

`let x = `**ref** 0 in

...
Access: operator

(**!**`! reference`

)

Assignment: operator

(**:=**```
reference
:= value
```

)

For instance: `x := !x + 1`

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`

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`

**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**

`expression`**;** expression

`begin end`

in the arms of a conditional `if then else`

.
**if** condition **then**
expression **else** expression

`if condition then `**begin** e1; e2
**end** else **begin** e3; e4
**end**

Standard comparison operators: ```
<, >, <=, >=,
<>
```

Physical equality: `==, !=`

**try** expression
**with** matching

Throwing:

**raise** constant_exception

or `raise `**(**exception_with_argument
expression**)**

- Screen:

Printing:`print_string`

,`print_int`

,`print_float`

, ..., or encore`printf "%d\n" 1`

- Keyboard:
`read_line`

- Files:
- for reading:
`open_in, close_in, input_line, input_string, input_char`

- for writing:
`open_out, close_out, output_string, output_char`

(structured data: input_value, output_value)

- for reading:

typename = | Constant_constructor | Constructor_with_argumentofexpression_de_type;;

Record type:

A field may be mutable, if declared as:typename = {label1 : type_of_the_field; label2 : type_of_the_field};;

**mutable** label : type_of_the_mutable_field

Assignment:

`record.label <- new_value`

Type abbreviation:

typename==type_expression;;

**exception** Constant_Exception;;

**exception** Exception_with_argument
**of** type_expression;;

Caml home page

Copyright © 1995 - 2004, INRIA all rights reserved.

Contact the author Pierre.Weis@inria.fr