Suppose I have a list of type integer [1; 2; 3; 4; 5; 6; 7; 8], and I want the template to match the first three elements at the same time. Is there a way to do this without nested matching operators?
for example, can this be done as follows?
let rec f (x: int list) : (int list) = begin match x with | [] -> [] | [a; b; c]::rest -> (blah blah blah rest of the code here) end
I could use a long nested method, which would be the following:
let rec f (x: int list) : (int list) = begin match x with | [] -> [] | h1::t1 -> begin match t1 with | [] -> [] | h2::t2 -> begin match t2 with | [] -> [] | t3:: h3 -> (rest of the code here) end end end
Thanks!
matching design-patterns ocaml elements
chesspro
source share