相当于OCAML中的Haskell $ $ operator

发布于 2025-02-12 02:41:31 字数 249 浏览 1 评论 0原文

OCAML中的Haskell的$运算符等效吗?还是我必须依靠支架?例如,请

multiplyByFive 5 + 1 -- 26

参见

multiplyByFive $ 5 + 1 -- 30

Is there an equivalent to Haskell's $ operator in OCaml, or do I have to rely on brackets? See for example,

multiplyByFive 5 + 1 -- 26

but

multiplyByFive $ 5 + 1 -- 30

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红玫瑰 2025-02-19 02:41:31

标准库同时定义了左侧的应用程序操作员@@

let compose h g f x = h @@ g @@ f x

和左右应用程序操作员|>

let rev_compose f g h x = x |> f |> g |> h

具有预期的关联性(适合@@ ,为|>左右。

The standard library defines both a right-to-left application operator @@

let compose h g f x = h @@ g @@ f x

and left-to-right application operator |>:

let rev_compose f g h x = x |> f |> g |> h

with the expected associativity (right for @@ and left for |>).

北渚 2025-02-19 02:41:31

您正在寻找@@

# let multiplyby5 a = 5 * a;;
val multiplyby5 : int -> int = <fun>
# multiplyby5 5 + 1;;
- : int = 26
# multiplyby5 @@ 5 + 1;;
- : int = 30

You're looking for @@

# let multiplyby5 a = 5 * a;;
val multiplyby5 : int -> int = <fun>
# multiplyby5 5 + 1;;
- : int = 26
# multiplyby5 @@ 5 + 1;;
- : int = 30
猫九 2025-02-19 02:41:31

在OCAML中,您可以使用应用程序操作员(在OCAML 4.01中添加)实现相同的操作。

multiplyByFive @@ 5 + 1
- : int = 30

应用程序操作员具有正确的优先级,因此首先评估操作员的右侧,类似于Haskell的应用程序操作员($)。您可能还想查看OCAML中的管道操作员(|&gt;),这是类似于Haskell中(&amp;)运算符的反向应用程序操作员。

In OCaml, you can use the application operator (added in OCaml 4.01) to achieve the same.

multiplyByFive @@ 5 + 1
- : int = 30

The application operator carries right precedence so the right side of the operator is evaluated first, similar to Haskell's application operator ($). You might also want to look into the pipeline operator (|>) in OCaml which is a reverse application operator similar to the (&) operator in Haskell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文