英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论