Reputation: 20725
How can I pass an operator to a function in ML? For example, consider this pseudocode:
function (int a, int b, operator op)
return a op b
Here, an operator can be op +
, op -
, etc. How can I do this in ML?
Upvotes: 6
Views: 1798
Reputation: 1768
Operators are just functions, once 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
Meaning that you can pass them as arguments to higher-order functions as you would with any other function.
Note that if you want to get an infix binding for a name, you can do the converse:
- val op* = String.^;
val * = fn : string * string -> string
- "foo" * "bar";
val it = "foobar" : string
In other words, the only `unusual' thing about infix operators is that they can be magically turned into regular (prefix) names by prepending 'op'.
Upvotes: 9