Cambridge University Press 1.1 Time Clock User Manual


 
44 CHAPTER 4. EFFECTIVE CAML
#make_vect n x;;
One can then read element m of a vector v using:
#vect_item v m;;
and write value y to element m of v using:
#vect_assign v m y;;
These operations correspond to the expressions v[m] and v[m] = y in C. The
elements of an array are numbered from zero. For example:
#let v = make_vect 5 0;;
v : int vect = [|0; 0; 0; 0; 0|]
#vect_item v 1;;
it : int = 0
#vect_assign v 1 10;;
it : unit = ()
#v;;
it : int vect = [|0; 10; 0; 0; 0|]
#vect_item v 1;;
it : int = 10
All reading and writing is constrained by bounds checking, e.g.
#vect_item v 5;;
Uncaught exception: Invalid_argument "vect_item"
4.3.3 Sequencing
There is no need for an explicit sequencing operation in CAML, since the normal
rules of evaluation allow one to impose an order. For example one can do:
#let _ = x := !x + 1 in
let _ = x := !x + 1 in
let _ = x := !x + 1 in
let _ = x := !x + 1 in
();;
and the expressions are evaluated in the expected order. Here we use a special
pattern which throws away the value, but we could use a dummy variable name
instead. Nevertheless, it is more attractive to use the conventional notation for
sequencing, and this is possible in CAML by using a single semicolon:
#x := !x + 1;
x := !x + 1;
x := !x + 1;
x := !x + 1;;