Reputation: 155
I'm new with OCaml. I'm making a function working this way : I have "tab" in the scope which represents a 2D map, and three parameters, x, y and u.
x and y represent the position of the player, and u the direction of the bomb (right, top left etc.). I want the function to update tab so that every cell which are not in the given direction are updated to 0.
Here is my code so far :
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)
On the line 6, I get the following error : "characters 20-71: Warning 10: this expression should have type unit." and I don't know why.
Could someone explain my error please ?
Have a good day !
Upvotes: 1
Views: 82
Reputation: 5108
The =
symbol is here to check an equality when not preceded by a let
.
If you want to change the value of one element of an array, you have to use <-
instead.
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
The error you got was that you returned booleans where unit
was expected.
Upvotes: 1