1

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 !

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I thought it was checking equality if inside a condition (such as if, while etc.) Thanks for your quick answer !
No, when typing if b then e1 else e2, it evaluates b as a boolean. That's why = returns a bool. It is different from C in that matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.