(* This is a comment in Caml *)
1 + 2 * 3 - 1 = 6
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;;
let ident = expression in
...
let ... and ident2 = expression2
and ident3 =
...
let rec x = ...
let f x = expression
let f x y = expression
let f = function matching
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)
f x
f (x) (y)
or f x y
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
function
matching,
try ... with
matching, or
match ... with
matching).
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.
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:
| (pattern, pattern) -> expression
| pattern as ident ->
expression
| constant_pattern |
constant_pattern -> expression
| character pattern ..
character pattern -> expression
let letter = function | `a` .. `z` | `A` .. `Z` -> true | _ -> false;;
| pattern when condition
-> expression
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
[| 1; 2; 3 |]
or make_vect
number_of_elements initial_value
v.(0)
v.(0) <- new_value
do_vect
, map_vect
for i = 0 to vect_length v - 1 do ... done
vect_length
, , blit_vect
[]
, [ 1; 2; 3
]
, or x :: l
match l with | [] -> ... | x :: l -> ...Assignment: a list is immutable.
do_list
, map
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
raise constant_exception
or raise (exception_with_argument
expression)
print_string
, print_int
,
print_float
, ..., or encore printf "%d\n" 1
read_line
open_in, close_in, input_line, input_string,
input_char
open_out, close_out, output_string, output_char
(structured data: input_value, output_value)
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
record.label <- new_value
Type abbreviation:
type name == type_expression;;
exception Constant_Exception;;
exception Exception_with_argument
of type_expression;;
Contact the author Pierre.Weis@inria.fr