Reputation: 13
I have an OCaml function that returns the value of a specified cell in a table. The function works correctly but I still get a warning stating:
Warning 8: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: ([],_)
even though I have in fact accounted for that value in my implementation here:
let cell_value([i;j],table) = match ([i;j],table) with
([],_) -> []
| (_,[]) -> []
| (_::_,_::_) -> List.nth (List.nth table (j-1)) (i-1);;
Like I said, the function returns the correct value, I'm just trying to get rid of the error. I'm still fairly new to OCaml, so any help would be greatly appreciated!
Upvotes: 0
Views: 513
Reputation: 111
Contrary to what you might believe, the problem is not coming from the pattern-matching introduced by the match
keyword but by another one you may not be aware of. Indeed, there is a pattern matching in the following fragment of your definition:
let cell_value ([i;j], table) = ...
because ([i; j], table)
actually contains the pattern [i; j]
. For this reason, this definition assumes that the first component of the pair given as argument to cell_value
is a list containing exactly two elements. In other words, this definition is equivalent to:
let cell_value = function ([i; j], table) -> ...
or also to:
let cell_value x = match x with ([i; j], table) -> ...
Now, you probably understand the warning issued by the compiler: if you provide a value of the form ([], table)
to your function, it will fail. The compiler is right:
# cell_value ([], []);;
Exception: Match_failure ("//toplevel//", 1, 14).
Upvotes: 7