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

Is there a more concise way of writing nested pattern matches in OCaml?

I find myself writing things like

match f x with
| Some x' -> f' x'
| None ->
  match g x with
  | Some x' -> g' x'
  | None ->
    match h x with
    | Some x' -> h' x'
    | None ->
      ...

Is there a cleaner way to write this kind of code?

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 :

In your example, you can write a function that tries a list of alternatives:

let rec try_with alts x = match alts with
  | [] -> None
  | (pred, follow) :: rest ->
     match pred x with
     | Some x -> follow x
     | None -> try_with rest x
...
try_with [f,f';g,g';h,h']

Alternatively, you can pattern match on tuples to test multiple patterns:

match f x, g x, h x with
| Some x, _, _ -> f' x
| _, Some x, _ -> g' x
| _, _, Some x -> h' x

And deep pattern matching is also possible (but doesn’t really match your example):

let f = function
| [ _; Either.Left [|x|]; Either.Right [_;_,_,y] ] -> x + y
| _ -> 0
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