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

huangapple go评论40阅读模式
英文:

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

问题

以下是您要翻译的部分:

我正在尝试在Moscow ML/SML上编写关于幻方的代码。每当我尝试运行代码时,我总是在第15行遇到以下错误:

! 类型冲突:类型为
!   int * int
! 不能具有类型
!   int * int * int
! 因为元组的组件数量不正确

问题一直出现在以下行上:
else place_number (next_position (i, j)) num m

以下是完整的在方格中放置数字的函数:

    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

我尝试过使用不同的实现方式,但基本上都导致相同的错误。其中一些是:

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)

英文:

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

得分: 1

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
英文:

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

huangapple
  • 本文由 发表于 2023年5月14日 03:26:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定