Sorting an array Imperative ocaml

527 views Asked by At

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 ;;
2

There are 2 answers

3
ivg On BEST ANSWER

First off all, there is no let x = y; expression in OCaml, a correct syntax is let x = y in, also you shouldn't forget to dereference your references.

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
    while !pos <> (Array.length 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 ;;
2
Pierre G. On

The following fix in the code may help - at least to get the code compiled - :

let sort toto =
        for index = 0 to (Array.length toto - 1) do
                let boole = ref false in
                let pos = ref index in
                let max = ref toto.(index) in
                let p = ref !pos in
                let m = ref !max in
                begin
                        while !pos <> (Array.length toto - 1 ) do
                        begin
                                if (toto.(!pos) > !max) then
                                begin
                                        max := toto.(!pos);
                                        boole := true;
                                        p := !pos;
                                end;
                                pos := !pos + 1;
                        end
                        done;
                        if (!boole = true) then begin
                                toto.(index) <- !max;
                                toto.(!pos) <- !m
                        end
                end
        done;;

Notably : the declaration of local variable, and also some missing semicolons. I change the name of the argument (array to toto) - as array is a keyword, but I do not think it is necessary.