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)
Your definition says that
place_number
takes two arguments; a triple(i, j, num)
andm
.Then you try to call it with three arguments;
(next_position (i, j))
,num
, andm
.Assuming that
next_position
has the typeint * int -> int * int
, you need to deconstruct its result first and then make a triple with the parts:but it's more convenient to change the function's type: