OCaml 中函数声明的语法
我想定义一个函数如下:
let f (a: int) (b: int) (c: int) (d: int): int =
...
是否可以在不使它们成为元组的情况下使签名更短?因为我仍然希望 f
有 4 个参数。
非常感谢。
Edit1:我只是认为重复 int
4 次是没有用的,想象一下类似 let f (a, b, c, d: int): int
目前实际上是不允许的。
I would like to define a function as following:
let f (a: int) (b: int) (c: int) (d: int): int =
...
Is it possible to make the signature shorter without making them a tuple? As I still want f
to have 4 arguments.
Thank you very much.
Edit1: I just think it is useless to repeat int
4 times, and image something like let f (a, b, c, d: int): int
which actually is not allowed at the moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个语法:
它并没有短多少,但是如果你有很多这样的语法,你可以定义
type arith4 = int ->;整数->整数->整数-> int
并使用该名称作为g
的类型注释。Try this syntax:
It's not much shorter, but if you have a lot of these, you can define
type arith4 = int -> int -> int -> int -> int
and use that name as type annotation forg
.我的 OCaml 很生疏,但我相信您可以通过声明自己的类型并将其解压到函数体中来做到这一点。
您还可以这样做:
My OCaml is rusty but I believe you can do that by declaring your own type and by unpacking it in the function body.
You can also do this: