2014-11-22 12 views
3

나는 명령형 언어로 ocaml을 사용하는 방법을 배우기보다 쉬운 예제를 사용하고 있습니다. 내 생각 엔 내가 세미콜론으로 엉망하지만 배열 정렬 명령형 ocaml

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

감사합니다 코드에서 어떤 실수를 찾을 수 없습니다.

편집 1 :

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

질문 : 왜 bubblesort algo를 사용하지 않습니까? 첫눈에 당신이 사용하고있는 골동품을 인식하지 못합니다. –

+0

@JoeGob 나는 방금 ocaml의 명령형 프로그래밍을 망설이고있다. 나는 최상의 알고리즘을 찾지 못했다. –

답변

3

먼저 OCaml에 let x = y; 표현이없고 정확한 구문은 let x = y in이며 참조를 역 참조하는 것을 잊어서는 안됩니다.

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

좋아요, 저는 문법으로 let을 사용해야한다는 것을 몰랐습니다. 고맙습니다! –

+0

'let x = y; '가 최상위 레벨에 있지만 표현식에는 없습니다. – newacct

+0

확실한 표현식이 아니라 최상위 레벨로 사용할 수있는 명령문입니다. – ivg

0

:

누군가가이 질문을 통해 오는 경우

, 내가 올바른 코드를 게시하도록하겠습니다 위의 경우에도 올바른 구문을 올바르게 배열을 정렬하지 않은 원인 코드에서 수정 프로그램을 다음과 같은 것이 도움이 될 수 있습니다 - 적어도 코드를 컴파일 얻을 : -

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

: 지역 변수의 선언, 또한 일부 누락 된 세미콜론. 나는 인수의 이름을 변경한다. (array to toto) - 배열은 키워드이기 때문에 필요하지 않다고 생각한다.

+0

다른 답변과 마찬가지로 잘 제안되었지만 downvoted ... 나는 이해하고 싶다. –

+0

, 올바른 OCaml 식별자를 사용하는 것을 고려해보십시오. 가장 좋은 방법은'ocp-indent'를 사용하는 것입니다. 또한'begin/end' 블록을'while' 구조체에 추가 할 필요가 없습니다. 이미 암시 적 블록을 포함하고 있기 때문입니다. 세 번째는'array','list'와 같은 이름이 OCaml에서 사용될 수 있으며,'toto'를 사용하는 것은 추악합니다. 힌트는 – ivg

+0

xx! 토토는 못생긴다 : 100 % 동의! –