0
votes

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?

1
Mmmh, why would you want to create a data mess like this? BTW there is a typo in let test = . And you can't match on it, because it has no structure (looks like that)Str.

1 Answers

1
votes

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.