【问题标题】:How to convert an argument of a function from int to real in sml?如何在sml中将函数的参数从int转换为real?
【发布时间】:2021-02-15 22:25:09
【问题描述】:

这是我在 sml 中编写的用于计算谐波和的代码。我基本上想计算实数 至于整数,所有的都将评估为 0(没有任何用处)。但是这段代码出错了。

试验一:

  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 也不起作用。请帮忙。

【问题讨论】:

    标签: sml


    【解决方案1】:

    我得到了我的问题的答案。 我们基本上需要使用

     if y<x then 0.0
      else f(x,y-1)+ 1.0/real(y);
    

    因为我们函数的类型是 (int*int)->real

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-13
      • 2012-01-08
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      相关资源
      最近更新 更多