Reputation: 13
I am having trouble pattern-matching tuples of varing lengths and types.
let test = ((6, 10), (3, "1", 9), ([2; "5"], 4, 7, "8"));;
let rec extract_min_int arg =
match arg with
| (a, b, c) ->
min (extract_lowest_int a) (min (extract_lowest_int b) (extract_lowest_int c))
| (a, b) -> min (extract_lowest_int a) (extract_lowest_int b)
| `int i -> i
| _ -> infinity
;;
extract_min_int test;;
I am expecting this function call to return 2, but I get the following error instead:
Error: This pattern matches values of type 'a * 'b but a pattern was expected which matches values of type 'c * 'd * 'e
I am fairly new to ocaml. This error is denying exactly what I am trying to do, which is match tuples of varying length/type.
What other options do I have here to accomplish this task?
Upvotes: 0
Views: 919
Reputation: 66803
OCaml is a strongly typed language. Each tuple size is a different type. So you can't write a function like you want.
If you have specific tuples of types in mind, you can define a variant type with just those combinations of types. This is what you would probably do in practice.
Upvotes: 1