(*
OK, OCaml just doesn't work with arrays the same way tha Perl does. In
Ocaml, Arrays are immutable in their shape, while containing mutable
contents. You can simulate this example as shown below (which only works for
string arrays), or you can get resizeable arrays from a library such as
extlib <http://ocaml-lib.sourceforge.net/>
*)let what_about_that_array a =
let len = Array.length a in
printf "The array now has %d elements.\n" len;
printf "The index of the last element is %d.\n"(if len=0then0else len-1);
printf "Element 3 is \"%s\".\n" a.(3);;let resizeArray a s =
(* begin stupid hack to work like the Perl example *)let s = s + 1in(* end stupid hack to work like the Perl example *)assert(s >= 0);let len = Array.length a inif s = len then a elseif s < len thenArray.sub a 0 s
elseArray.append a (Array.make (s - len)"");;let people = [|"Crosby";"Stills";"Nash";"Young"|];;
what_about_that_array people;;(*
The array now has 4 elements.
The index of the last element is 3.
Element 3 is "Young".
*)let people = resizeArray people 2;;
what_about_that_array people;;(*
The array now has 3 elements.
The index of the last element is 2.
Exception: Invalid_argument "index out of bounds".
*)let people = resizeArray people 10000;;
what_about_that_array people;;(*
The array now has 10001 elements.
The index of the last element is 10000.
Element 3 is "".
*)

(* To reverse a list, use List.rev *)let reversed = List.rev l;;(* For an array, it is probably easiest to use Array.init *)let revArray a =
let len = Array.length a - 1inArray.init len+1(fun i -> a.(len - i);;let reversed = revArray a;;(* Or one can use a for loop *)for i = Array.length a - 1 downto 0do(* Do something to a.(i) *)done;;