如何在OCAML中返回浮子?
我已经在OCAML中编写了此简单功能以计算列表的总和:
let rec sum lst =
match lst with
| [] -> 0.0
| h :: t -> h + sum t
但是,当我调用它时,我会收到错误:
Error: This expression has type float
but an expression was expected of type int
如何重写此功能,以便它能够总和一个floats的列表并返回零作为float( 0.0)如果列表为空?
I have written this simple function in OCaml to calculate the sum of a List:
let rec sum lst =
match lst with
| [] -> 0.0
| h :: t -> h + sum t
However I receive an Error when I call it:
Error: This expression has type float
but an expression was expected of type int
How can I rewrite this function so it is able to sum a List of floats and return a zero as a float (0.0) if the List is empty?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在OCAML Integer数学中,使用
+
,-
,*
和/
。 - 。,*。是用
+。
,浮点数学 您可以写以下内容。不需要浮动点文字上的尾随
0
。这具有成为尾声的好处。In OCaml integer math is done with
+
,-
,*
, and/
. Floating point math is done with+.
,-.
,*.
, and/.
You want:
Although you could just write the following. The trailing
0
on floating point literals is not required. This has the benefit of being tail-recursive.