I am trying to figure out how to match on two arguments in ocaml.
I have a function that takes in two tuples:
let time a b = ...;;
time (34, 4)(5, 6);; // function call
How can I access, say, the first item in the first tuple and add it with the first item in the second tuple? Thanks!
>Solution :
let time (a, _) (b, _) = a + b;;
or, to destructure any binding outside of function arguments:
let time a b =
let a', _ = a in
let b', _ = b in
a' + b'
In general you’ll find that most patterns look exactly like how you’d construct the value. Pretty handy that way.