Type clash expression of type int * int cannot have type int * int * int on SML

43 views Asked by At

I'm trying to make a code on Moscow ML/SML about magic squares. Whenever I try to run the code, I always get this error on line 15:

! Type clash: expression of type
!   int * int
! cannot have type
!   int * int * int
! because the tuple has the wrong number of components

With the following line being the problem constantly: else place_number (next_position (i, j)) num m

Here's the complete function that places a number in a position within the square:

    fun place_number (i, j, num) m =
      if Array.sub(Array.sub(m, i), j) = 0 then
        (Array.update(Array.sub(m, i), j, num); m)
      else place_number (next_position (i, j)) num m

I've tried to use different implementations of it but it pretty much results on the same error. A few are:

else place_number (next_position (i, j)) num m

else place_number (next_position (i, j)) num

else place_number (next_position (i, j)) num (m)

1

There are 1 answers

0
molbdnilo On

Your definition says that place_number takes two arguments; a triple (i, j, num) and m.

Then you try to call it with three arguments; (next_position (i, j)), num, and m.

Assuming that next_position has the type int * int -> int * int, you need to deconstruct its result first and then make a triple with the parts:

fun place_number (i, j, num) m =
    if Array.sub(Array.sub(m, i), j) = 0 then
        (Array.update(Array.sub(m, i), j, num); m)
    else 
        let val (i', j') = next_position (i, j)
          in place_number (i', j', num) m
        end

but it's more convenient to change the function's type:

fun place_number (i, j) num m =
      if Array.sub(Array.sub(m, i), j) = 0 then
        (Array.update(Array.sub(m, i), j, num); m)
      else place_number (next_position (i, j)) num m