Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pattern matching on two arguments in ocaml

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading