gpt4 book ai didi

SML-错误 : operator is not a function [tycon mismatch]

转载 作者:行者123 更新时间:2023-12-03 09:12:24 31 4
gpt4 key购买 nike

我正在尝试创建一个程序来对大阶乘的数字求和,这就是我正在做的事情:

fun sumDigits n = 
if n < 10 then n
else
n mod 10 + sumDigits(n div 10)


fun factLarge 1 = IntInf.toLarge 1
| factLarge n = IntInf.toLarge n * factLarge(n-1)


sumDigits (factLarge 100)

但是我在 sumDigits (factLarge 100) 上遇到错误,而且我不知道如何修复它。

20.sml:8.19-11.26 错误:运算符不是函数 [tycon 不匹配] 运算符:IntInf.int 在表达式中: (factLarge (n - 1)) sumDigits

最佳答案

该特定错误是由于您必须将代码粘贴到 REPL 中的方式造成的。它无法弄清楚 factLarge 的定义在哪里结束。在该定义的末尾添加一个分号,此错误就会消失(或者,最好使用命令 use filename.sml; 而不是复制粘贴代码):

fun sumDigits n = 
if n < 10 then n
else
n mod 10 + sumDigits(n div 10);

fun factLarge 1 = IntInf.toLarge 1
| factLarge n = IntInf.toLarge n * factLarge(n-1);

sumDigits (factLarge 100);

不幸的是,这将表面的错误换成了更深层次的错误:

stdIn:40.1-40.26 Error: operator and operand don't agree [tycon mismatch]
operator domain: int
operand: IntInf.int
in expression:
sumDigits (factLarge 100)

问题是您的 sumDigits 需要一个 int,而不是 IntInf.int。您必须在 sumDigits 的定义上添加适当的类型注释才能使其正常工作。由于这似乎是家庭作业,所以我将其留给您完成。

关于SML-错误 : operator is not a function [tycon mismatch],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40932329/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com