基本循环中的 OCaml 错误
OCaml error in basic loop
我是 OCaml 新手。
我正在制作一个以这种方式工作的函数:我在代表二维地图的范围内有 "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 > 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:这个表达式应该有类型单位。”我不知道为什么。
有人可以解释我的错误吗?
祝你有个愉快的一天!
=
符号用于检查前面没有 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
的位置返回了布尔值。
我是 OCaml 新手。 我正在制作一个以这种方式工作的函数:我在代表二维地图的范围内有 "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 > 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:这个表达式应该有类型单位。”我不知道为什么。
有人可以解释我的错误吗?
祝你有个愉快的一天!
=
符号用于检查前面没有 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
的位置返回了布尔值。