| Newsgroups |
gmane.comp.lang.ocaml.beginners |
| Message-ID |
<CAPFanBHEoiq2XB=Y+Au=P-g9-U9bZn_HbX9S3zNWPT0qzExnYw@mail.gmail.com> |
When you write
| e ->
the pattern "e" is interpreted as a variable name to give to the
scrutinee (the value being matched), just as "h" and "t" in "h::t". It
does not matter that a variable of the name "e" already exists, and
OCaml does not check for equality.
This is why you get a warning "This match case is unused": because the
first case just gives a name to the value, it always succeeds, and the
second case will never be selected.
To avoid this mistake (after having presented an example of exactly
this error), I often ask my students to respect the following rules:
- all cases in a pattern must start with a constructor (in particular
"_" as pattern is disallowed)
- no pattern-matching on base types (ints, strings, floats, etc.)
That may be also an answer to your question of "when to use
pattern-matching vs. if then else". With time, you get to understand
pattern-matching better, and those rules can be relaxed -- when
relaxing them helps readability without decreasing the robustness to
change or maintainability of the code.
On Fri, Oct 9, 2015 at 5:55 AM, Seung-jin Kim [email protected]
[ocaml_beginners] <[email protected]> wrote:
> First of all,
> There was a typo in my very initial question.
> my has_element1 should be
>
> let rec has_element1 l e =
> match l with
> | [] -> false
> | h::t -> if e = h then true else has_element1 t e;;
>
> Anyway,, Seems everyone got my point. :-) First time to post this
> group and very new to ocaml.
>
> I did with ( ) for my second match.
>
> utop[91]> let rec has_element2 l e =
> match l with
> | [] -> false
> | h::t -> ( match h with
> | e -> true
> | _ -> has_element2 t e
> )
> ;;
>
>
> val has_element2 : 'a list -> 'b -> bool = <fun>
> Characters 107-108:
> Warning 11: this match case is unused.
> utop[92]> has_element2 [2;3;4] 10;;
> - : bool = true
> utop[93]>
>
> Still getting the same warning message with the same result.
>
>
> On 10/08 11:19 PM, Hendrik Boom [email protected] [ocaml_beginners] wrote:
>> On Thu, Oct 08, 2015 at 08:12:48PM -0700, [email protected] [ocaml_beginners] wrote:
>> > Code first:
>> >
>> >
>> > let rec has_element1 l e =
>> > match l with
>> > | [] -> false
>> > | h::t -> if e = h then true else false
>> > ;;
>> >
>> > let rec has_element2 l e =
>> > match l with
>> > | [] -> false
>> > | h::t -> match h with
>> > | e -> true
>> > | _ -> has_element2 (List.tl l) e
>> > ;;
>> >
>> >
>> >
>> > has_element1 works as I expected.
>> >
>> > And I rewrite has_element1 with replacing 'if expressing' with 'match.
>> >
>> > But has_element2 always return 'true' and it gives me warning: 'Warning 11: this match case is unused'
>> >
>> >
>> > Questions:
>> >
>> > (1) Why does my has_element2 always return true? Match expression I
>> > used in has_element2 not the same as if expression in has_element1?
>>
>> How is the poor compiler to know whether the last alternative belongs
>> with the first or the second match? Try inserting ( before the second
>> match keyword and putting its ")" where it belongs.
>>
>> I think this may be a language design problem. But the problem ends up
>> being caught in the type analysis, so it's probably only confusing.
>>
>> When OCaml misbehaves I usually find it enlightening to be explicit
>> about types and parentheses.
>>
>> -- hendrik
>
> Seungjin Kim
>
>
> ------------------------------------
> Posted by: Seung-jin Kim <[email protected]>
> ------------------------------------
>
> Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.
> ------------------------------------
>
> Yahoo Groups Links
>
>
>