作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个程序来对大阶乘的数字求和,这就是我正在做的事情:
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/
我是一名优秀的程序员,十分优秀!