You could pattern-match against multiple arguments of a function by creating a tuple and then destructuring it in a match expression:
let f x y =
match x, y with
| pattern1 -> expr1
| ...
Alternatively, if you don’t need a curried function, you could do this by making f take a tuple as the only argument:
let f (x, y) = function
| pattern1 -> expr1
| ...
The advantage of the latter method is that you don’t have to write the arguments twice every time you define a function. But functions that take a tuple seems to be not as popular than curried ones.
So which of the two is deemed canonical, or preferred, in the OCaml community?
EDIT: Just as pad pointed out below, I mean let f = function blah blah in the second code snippet.
A tuple is not just a syntactic construct, it represents a real data structure. This means that
fun (x,y)is (very slightly) less efficient thanf x yin the case werexandyaren’t already tupled, because a tuple has to be allocated. If this isn’t clear, the rough equivalent in Java would beFor this reason, it’s generally preferable to avoid using tuples as function arguments unless you have a good reason to do so.
P.S. A similar consideration applies to datatype declarations, where the following are subtly different:
Foois a datatype constructor that takes two arguments.Baris a constructor that takes one argument (a tuple).