hqt
hqt

Reputation: 30276

SML : why functions always take one-argument make language flexible

I have learned (from a SML book) that functions in SML always takes just one argument: a tuple. A function that takes multiple arguments is just a function that takes one tuple as argument, implemented with a tuple binding in function binding. I understand this point.

But after this, the book says something that I don't understand:

this point makes SML language flexible and elegant design, and you can do something useful that you cannot do in Java.

Why does this design make the language Flexible? What is the text referring to, that SML can but java cannot?

Upvotes: 4

Views: 1393

Answers (2)

Jesper.Reenberg
Jesper.Reenberg

Reputation: 5944

Actually I don't think you really understand this at all.

First of all, functions in SML doesn't take a tuple as argument, they can take anything as argument. It is just sometimes convenient to use tuples as a means of passing multiple arguments. For example a function may take a record as argument, an integer, a string or it may even take another function as argument. One could also say that it can take "no arguments" in the sense that it may take unit as the argument.

If I understand your statement correctly about functions that takes "multiple arguments" you are talking about currying. For example

fun add x y = x + y

In SML, currying is implemented as a derived form (syntactic sugar). See this answer for an elaboration on how this actually works. In summary there is only anonymous functions in SML, however we can bind them to names such that they may "referred to"/used later.

Behold, ramblings about to start.

Before talking about flexibility of anything, I think it would be in order to state how I think of it. I quite like this definition of flexibility of programming languages: "[...] the unexpectedly many ways in which utterings in the language can be used"

In the case of SML, a small and simple core language has been chosen. This makes implementing compilers and interpreters easy. The flexibility comes in the form that many features of the SML language has been implemented using these core language features such as anonymous functions, pattern matching and the fact that SML has higher-order functions.
Examples of this is currying, case expressions, record selectors, if-the-else expressions, expression sequences.

I would say that this makes the SML core language very flexible and frankly quite elegant.

I'm not quite sure where the author was going regarding what SML can do, that java can't (in this context). However I'm quite sure that the author might be a bit biased, as you can do anything in java as well. However it might take immensely amounts of coding :)

Upvotes: 3

sepp2k
sepp2k

Reputation: 370162

Using tuples instead of multiple arguments adds flexibility in the sense that higher-order functions can work with functions of any "arity". For example to create the list [f x, f y, f z], you can use the higher-order function map like this:

map f [x, y, z]

That's easy enough - you can do that in any language. But now let's consider the case where f actually needs two arguments. If f were a true binary function (supposing SML had such functions), we'd need a different version of map that can work with binary functions instead of unary functions (and if we'd want to use a 3-ary functions, we'd need a version for those as well). However using tuples we can just write it like this:

map f [(x,a), (y,b), (z,c)]

This will create the list [f (x,a), f (y,b), f (z,c)].

PS: It's not really true that all functions that need multiple arguments take tuples in SML. Often functions use currying, not tuples, to represent multiple arguments, but I suppose your book hasn't gotten to currying yet. Curried functions can't be used in the same way as described above, so they're not as general in that sense.

Upvotes: 6

Related Questions