Sorting the Imperative ocaml array

I am making a fairly simple example to learn how to use ocaml as an imperative language. I am assuming I messed up the semicolon, but I cannot find any errors 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.

Edit 1:

In case someone comes across this question, I am posting the correct code, because the above did not 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 ;;

      

+3


source to share


2 answers


First of all, there is no expression in OCaml let x = y;

, correct syntax let x = y in

, and you should also remember to dereference your links.



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

      

+3


source


The following fix in the code might help - at least compile the code -:

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

      



In particular: a local variable declaration, as well as some missing semicolons. I change the name of the argument (array toto) - since array is a keyword, but I don't think it is necessary.

0


source







All Articles