“Marcin ‘Qrczak’ Kowalczyk” qrczak@knm.org.pl wrote in message

f Ada, Eiffel, Haskell, Mercury, Perl, Pliant, Ruby, Tcl, Pascal

f() Awk, C, C#, C++, Java, Lua, Python

f() merd, OCaml, SML (there really is a parameter which is the empty

tuple)

You can also add OCaml to the first line but in this case the function would

only ever be evaluated once - henceforth the return value would be used

(strict evaluation).

Be careful with OCaml and parentheses:

For the sake of example lets define the min and max functions first:

let min a b = a < b then a else b;; let max a b = a > b then a else b;;

OCaml does not have parentheses around function arguments. arguments follow

the function separated by space: “min 2 3” this yields “2” as expected.

C-style “min(x, max(y, z))” becomes “min x (max y z)” in OCaml. The

parentheses are used to correctly group the arguments. Really “(max y z)”

means the single value tuple made up of the return value from max - but a

single value is the same as the single value tuple so the parentheses works

like normal parentheses grouping.

“min(2,3)” yields something strange - a partially evaluated function taking

a two integer tuple as argument as if it was defined like

“let min_strange (x, y) = if (2, 3) < (x, y) then (2, 3) else (x, y);;”

(you can compare tuples, so the function makes sense).

You could also define min to work on a two tuple:

“let min’ (a, b) = a < b then a else b” and you would get the expected

result when writing “min(2,3)”. This would be the same as writing a Ruby

function expecting an array as argument to a min function.

It’s correct that the empty tuple () can be applied to a function. But a

function can also be called without arguments at al (depending on its type).

A function without arguments evaluate to a constant and is only executed

once. There you sometimes write functions that take dummy arguments such as

the empty tuple to allow multiple evalutions (which is useful when printing,

say, linebreaks to an out stream).

The arguments are applied to OCaml differs from standard ML (SML) and it is

primary source of trouble to newcomers (i.e. me). (Someone said OCaml is big

on currying or something).

There is a reason for OCamls strange syntax: partially evaluated functions

and higher order functions (functions taking functions as arguments) becomes

very natural to work with.

If a function isn’t provided with all arguments, the return value is a new

function that accepts the remaining arguments: “(min 2)” yields a new

function taking one integer and returns the smaller value of 2 and the

argument: “(min 2) 4” is the same as “min 2 4” but it happened was evaluated

in two steps (in bytecode this may matter, in assembly it’s optimized away).

In conclusion: In OCaml you generally put the left parentheses before the

function name, rather than after it. This may look at bit like Lisp, but

isn’t quite.

Tuples are very powerful datastructures in OCaml. They are not lists and

they are not arrays. They do, for example, make it much easier to handle

values in parser reductions. In Ruby you would use arrays or hashes, but

they have a much higher overhead. It would be nice with tuples in Ruby.

Mikkel