基本循环中的OCaml错误
我是OCaml的新手。 我让一个函数以这种方式工作:在表示2D地图的范围内有“tab”,还有三个参数,x、y和u x和y代表玩家的位置,u代表炸弹的方向(右、左上等)。我希望函数更新选项卡,以便不在给定方向上的每个单元格都更新为0 以下是我目前的代码:基本循环中的OCaml错误,ocaml,Ocaml,我是OCaml的新手。 我让一个函数以这种方式工作:在表示2D地图的范围内有“tab”,还有三个参数,x、y和u 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 >
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:这个表达式应该有unit类型。我不知道为什么
有人能解释一下我的错误吗
祝你有愉快的一天 此处的
=
符号用于在前面没有let
时检查是否相等。
如果要更改数组中一个元素的值,必须使用
对于i=0到(w-1)do
对于j=0到(h-1)do
如果i>x那么
如果j>y那么
tab.(i)。(j)我以为它是在检查条件(如if、while等)内是否相等。谢谢您的快速回答!否,当键入if b然后e1 else e2
时,它将b
计算为布尔值。这就是为什么=
返回bool
。在这方面它与C不同。
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