I want to strengthen a pattern to match only numbers which pass an additional validation function.
let (|IsValid|_|) n = ...
let (|Nil|One|Two|) (l : int list) =
match l with
| a :: b :: t -> Two(a + b)
| a :: t -> One(a)
| _ -> Nil
The 'One' case is easy:
| IsValid(a) :: t -> One(a)
The 'Two' case isn't obvious to me. It needs to validate the sum of the numbers. Can I do this without using a when-guard?
...
Edit: I could use a when-guard (with a bool-returning isValid function) like this:
| a :: b :: t when isValid a + b -> Two(a + b)
This is less elegant than just matching a pattern; worse, a + b is applied twice.
Also note this is a simplified version of my actual code (I'm not trying to simply match against different lengths of list for example) - the question is about nested matching over the double cons pattern.