Reputation: 95
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
Upvotes: 8
Views: 800
Reputation: 416
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.
Upvotes: 7
Reputation: 18912
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 |>
).
Upvotes: 16
Reputation: 4441
You're looking for @@
# let multiplyby5 a = 5 * a;;
val multiplyby5 : int -> int = <fun>
# multiplyby5 5 + 1;;
- : int = 26
# multiplyby5 @@ 5 + 1;;
- : int = 30
Upvotes: 7