Is the o
composition operator (eg. val x = foo o bar
, where foo
and bar
are both functions), only usable on single-argument functions and/or functions with equal numbers of arguments? If not, what is the syntax for, say, composing foo(x,y)
with bar(x)
.

- 7,274
- 1
- 32
- 50

- 1,300
- 3
- 16
- 25
3 Answers
As Michael already said, yes, SML only has single argument functions. I want to elaborate a bit, though.
The following function:
fun foo (x,y) = x + y
Has the type:
fn : int * int -> int
Which means that the first argument is a tuple of two ints. So you could do something like:
(sign o foo) (4,~5)
Which would give you the same as sign (foo (4,~5))
.
Okay, but what about something like this?
fun bar x y = x + y
It has the type:
fn : int -> int -> int
Which means that bar actually takes just one integer, and returns a function. So you can't do this:
(sign o bar) 4 ~5
Because bar returns a function, and sign takes an integer. You can do this, though:
(sign o bar 4) ~5
Because bar 4
is a function that adds 4 to a number.

- 1,896
- 11
- 15
-
2If you defined a `uncurry` function to help facilitate your last "problem" with `sign o bar`, and possibly also a `flip` function that can flip "argument ordering" by flipping the pair `(x,y)` to `(y,x)`, etc. – Jesper.Reenberg Feb 04 '13 at 23:23
-
Jesper: In this case, I'd probably define a $ operator and do `sign $ bar x y`. – Tayacan Feb 05 '13 at 16:22
SML only has single argument functions; foo(x,y)
is a function foo
taking a single argument, the tuple (x, y)
. As such, there is no special handling needed and bar(x)
will need to return a tuple of the appropriate type to compose it with foo
.

- 24,518
- 9
- 68
- 88
I've seen some Standard ML code (notably the Poly/ML code Isabelle/Pure) which puts extra composition operators into the top-level environment which handle this kind of situation. E.g.:
fun (f oo g) x y = f (g x y)
fun (f ooo g) x y z = f (g x y z)
fun (f oooo g) x y z w = f (g x y z w)
Generally, such things should be used sparingly (four or more o's is getting a bit silly), but it is quite useful having at least oo
around.

- 183
- 3