Skip operator to work in ML

How to pass a function operator to ML? For example, consider this pseudo code:

function (int a, int b, operator op)
    return a op b

Here the operator may be op +, op -etc. How to do it in ML?

+4
source share
1 answer

Operators are just functions as soon as you prefix them with "op". So you can treat them like any old function:

Standard ML of New Jersey v110.75 [built: Sat Nov 10 05:28:39 2012]
- op+;
val it = fn : int * int -> int

This means that you can pass them as arguments for functions of a higher order, as well as for any other function.

Note that if you want to get the infix binding for the name, you can do the opposite:

- val op* = String.^;
val * = fn : string * string -> string
- "foo" * "bar";
val it = "foobar" : string
In other words, the only “unusual” thing in infix operations is that they can be magically turned into regular (prefix) names by adding “op”.
+8
source

All Articles