问题描述
这是我用sml写的计算调和和的代码。我基本上想计算实数 至于整数,都将评估为 0(没有任何用处)。但是这段代码出错了。
试验 1:
if y<x then 0
else f(x,y-1)+ 1/y;
错误:
Elaboration Failed: Type clash. Functions of type "real * real → real" cannot take an argument of type "int * int": Cannot merge "int" and "real".
试验 2:
if y<x then 0
else f(real(x),y-1)+ 1/y;
错误:
Elaboration Failed: "real" is not a constructor.
即使用 0.0 替换 0 也没有用。请帮忙。
解决方法
我得到了问题的答案。 我们基本上需要使用
if y<x then 0.0
else f(x,y-1)+ 1.0/real(y);
因为我们函数的类型是 (int*int)->real