Ian Fako
Ian Fako

Reputation: 1210

How can I do pattern matching on tuple constructors (OCaml)

I have the following types:

type letter = A | B | C | D (*...*)
type mix = Char of letter | Mix of (mix * int) list

I want to make a function, which counts the number of occuriencies of a letter in a mix, but struggling to do the pattern matching right.

let rec count_char letter mix = match mix with
    | Char l -> (*...*)
    | Mix (m, i) -> (*...*)

I am getting this error

Error: This pattern matches values of type 'a * 'b
       but a pattern was expected which matches values of type
       (mix * int) list

Upvotes: 0

Views: 1026

Answers (1)

glennsl
glennsl

Reputation: 29106

It's not that it's a tuple, it's that it is a list of tuples that you're trying to match against a single tuple. Mix ((m, i) :: _) will work, but will of course result in a partial match unless you also have a branch that matches the empty list.

Upvotes: 1

Related Questions