I'm doing a rather easy example to learn how to use ocaml as an imperative language. My guess is I messed up with the semicolons but I can't find any mistakes in the code
let sort array =
for index = 0 to (Array.length array -1) do
let boole = ref false;
let pos = ref index;
let max = ref array.(index);
let p = ref !pos;
let m = ref !max;
while !pos <> (Array.lenght array -1 ) do
if array.(!pos) > !max then begin
max := array(!pos);
boole := true;
p := !pos
end
pos := !pos + 1
done;
if (!boole = true) then begin
array.(index) <- max;
array.(pos) <- m
end
done ;;
Thank you.
Edit 1 :
In case someone comes across this question, I'm posting the correct code cause the above didn't sort the array correctly even with the correct syntax:
let sort array =
for index = 0 to (Array.length array -1) do
let boole = ref false in
let pos = ref index in
let max = ref array.(index) in
let p = ref !pos in
let m = ref !max in
for i = !pos to (Array.length array -1) do
if (array.(i) > !max) then begin
pos :=i;
max := array.(!pos);
boole := true;
end;
done;
if (!boole = true) then begin
array.(!pos) <- !m;
array.(!p) <- !max;
end;
done ;;
First off all, there is no
let x = y;
expression in OCaml, a correct syntax islet x = y in
, also you shouldn't forget to dereference your references.