3

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 ;;
Álvaro
  • 105
  • 1
  • 9
  • question : why not using bubblesort algo ? At first sight I fail to recognize the algo you are using. – Pierre G. Nov 24 '14 at 10:22
  • @JoeGob I'm just messing around with imperative programming in ocaml, I wasn't looking for the best algorithm – Álvaro Nov 24 '14 at 10:58

2 Answers2

3

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 ;;
ivg
  • 34,431
  • 2
  • 35
  • 63
0

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.

Pierre G.
  • 4,346
  • 1
  • 12
  • 25
  • well - same proposal as the other answer - but downvoted... I would have like to understand. – Pierre G. Nov 22 '14 at 18:26
  • please, consider using proper OCaml identation, the best is to use `ocp-indent`. Also, there is no need to add this `begin/end` blocks into `while` constructs, since they already contains implicit blocks. The third, names like `array`, `list` etc can be used in OCaml, and using `toto` is ugly. – ivg Nov 24 '14 at 11:15