RE: match case unused
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Thanks, The exercise is not explicite said to use match but the exercise is stated as this : Rewri t e t h e not f u n ct ion f rom t h e previou s ch apt er in pat t ern m at ch in g st y le The biggest problem I have is mostly that I understand if I read about things but have struggles to use it in practice. Roelof To: [email protected] From: [email protected] Date: Fri, 27 Feb 2015 18:10:43 +0000 Subject: Re: "ocaml_beginners"::[] match case unused 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