sml - 如何在sml中将函数的参数从int转换为real?
问题描述
这是我在 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".
试验二:
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
推荐阅读
- tensorflow - Weight policy for imbalanced tensorflow model
- json - 如何选择名字等于孩子名字的父母
- java - How to Query a Postgresql table with spaces in java
- python - In torch.distributed, how to average gradients on different GPUs correctly?
- c++ - 从类模板继承所需的 iostream
- java - Java中的yaml生成
- javascript - 根据屏幕尺寸缩放多个元素
- amazon-web-services - AmazonSQSException:参数 Condition 的值 aws:arn 必须来自全局上下文键列表
- c# - 使用最小起订量的扩展方法的单元测试
- java - SQLiteAssetHelper 中缺少数据库错误