Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an infix (not symbolic aka not an operator) function in OCaml?

does OCaml support infix functions defined in plaintext ?

arg1 `plus` arg2 = arg1 + arg2 

thanks

like image 752
desmogix Avatar asked Jul 01 '16 14:07

desmogix


People also ask

How do you define an operator in ocaml?

A custom prefix operator can made by from a ! followed by one or more symbol characters. and is followed by one or more symbol characters. For example let ( ~! )

What is a infix function?

User defined infix function notation –It must be member function or extension function. It must accepts a single parameter. The parameter must not accept variable number of arguments and must have no default value. It must be marked with infix keyword.

Is an infix operator?

An infix operator is a function of two arguments, with the name of the function written between the arguments. For example, the subtraction operator - is an infix operator.

How do you define an infix function in Haskell?

There's no way to define a function with an alphanumeric name as infix. Haskell's syntax rules only allow for functions with symbolic names or function names surrounded with backticks to be used infix - there's no way to change that.


1 Answers

No. As explained in the OCaml manual, infix operators can only contain special characters, namely !, $, %, &, *, +, -, ., /, :, <, =, >, ?, @, ^, |, ~ (or # but only in first position) and must not start with !, ? or ~.

In order to define an infix operation, you must put the symbol into parentheses:

# let (+++) x y = x + 2 * y;;
...
# 3 +++ 4;;

- : int = 11
like image 61
Virgile Avatar answered Oct 07 '22 06:10

Virgile