Reputation: 43
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!
Upvotes: 0
Views: 581
Reputation: 36630
In addition to the answer from @glennsl, you may find as
useful.
let time (a', _ as a) (b', _ as b) =
(* ... *)
This lets you destructure both tuples, but also bind a name to each tuple.
Upvotes: 2
Reputation: 29126
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.
Upvotes: 4