Home > Blockchain >  Equivalent of haskell's $ operator in ocaml
Equivalent of haskell's $ operator in ocaml

Time:07-01

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

CodePudding user response:

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 |>).

CodePudding user response:

You're looking for @@

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

CodePudding user response:

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.

  • Related