Re: match case unused

"Dario Teixeira [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Hi,


> I have to rewrite a not function with pattern matching.> 
>So I did this : 
> 
>let not x = 
>   match x with 
>     x -> false
>     | _ -> true ;;
> 
>but now I see this warning : Warning 11: this match case is unused.


The first match case, "x", matches everything, which is why the compiler
is rightfully telling you that the second match case will never be used.

You may have a common misunderstanding among beginners: confusing the
pattern in a "match" expression with let-bindings and/or arguments with
the same name.  Note that if you rename that first pattern to "whatever",
then the problem with the function becomes obvious:

  let not x = match x with
    | whatever -> false
    | _        -> true

What you actually want is probably something like this:

  let not x = match x with
    | true  -> false
    | false -> true

(As you probably realise this is a silly way to write a "not" function,
but if the exercise explicitly demands using "match", so be it).

Anyway, you seem bogged down by very "beginnery" problems.  Make sure
to read and re-read the first chapters of the book you're following
until you're 100% confident you've understood it all.

Best regards,
Dario Teixeira
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.