2015-06-14 40 views
1

我是OCaml的新成員。 我在做一個這樣工作的函數:我在代表2D地圖的範圍中有「tab」,還有三個參數x,y和u。基本循環中的OCaml錯誤

x和y代表玩家的位置,u代表炸彈的方向(右,左上等)。我希望讓每一個細胞不屬於在給定方向被更新爲0

功能更新標籤這裏是我到目前爲止的代碼:

let test = fun x y u -> 
(for i = 0 to (w-1) do 
    for j = 0 to (h-1) do 
     if i > x then 
      if j > y then 
       tab.(i).(j) = (if u = "DR" then tab.(i).(j) else 0) 
      else 
       if j = y then 
        tab.(i).(j) = (if u = "R" then tab.(i).(j) else 0) 
       else 
        tab.(i).(j) = (if u = "UR" then tab.(i).(j) else 0) 
     else 
      if i = x then 
       if j > y then 
        tab.(i).(j) = (if u = "D" then tab.(i).(j) else 0) 
       else 
        tab.(i).(j) = (if u = "U" then tab.(i).(j) else 0) 
      else 
       if j > y then 
        tab.(i).(j) = (if u = "DL" then tab.(i).(j) else 0) 
       else 
        if j = y then 
         tab.(i).(j) = (if u = "L" then tab.(i).(j) else 0) 
        else 
         tab.(i).(j) = (if u = "UL" then tab.(i).(j) else 0) 
    done 
done) 

在第6行,我得到以下錯誤: 「字符20-71: 警告10:此表達式應具有類型單位。」我不知道爲什麼。

有人可以解釋我的錯誤嗎?

祝您有美好的一天!

回答

1

=符號在這裏檢查一個平等,當沒有之前let。 如果要更改數組中某個元素的值,則必須改用<-

let test = fun x y u -> 
    for i = 0 to (w-1) do 
    for j = 0 to (h-1) do 
     if i > x then 
     if j > y then 
      tab.(i).(j) <- (if u = "DR" then tab.(i).(j) else 0) 
     else 
      if j = y then 
      tab.(i).(j) <- (if u = "R" then tab.(i).(j) else 0) 
      else 
      tab.(i).(j) <- (if u = "UR" then tab.(i).(j) else 0) 
     else 
     if i = x then 
      if j > y then 
      tab.(i).(j) <- (if u = "D" then tab.(i).(j) else 0) 
      else 
      tab.(i).(j) <- (if u = "U" then tab.(i).(j) else 0) 
     else 
      if j > y then 
      tab.(i).(j) <- (if u = "DL" then tab.(i).(j) else 0) 
      else 
      if j = y then 
       tab.(i).(j) <- (if u = "L" then tab.(i).(j) else 0) 
      else 
       tab.(i).(j) <- (if u = "UL" then tab.(i).(j) else 0) 
    done 
    done 

你得到的錯誤是你回來,在那裏unit預計布爾值。

+0

我認爲這是檢查平等,如果在一個條件(如如果,而等) 感謝您的快速答案! –

+0

不,如果輸入'if b then e1 else e2',它會將'b'評估爲布爾值。這就是爲什麼'='返回一個'bool'。這與C有所不同。 –