Search code examples
smlsmlnjml

Is the SML `o` operator only useful on single-argument functions?


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


Solution

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