Re: if expression and match expression

"Gabriel Scherer [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAPFanBF4CS4Q3qAJE7D2Z92PMqXem695itBLL4=FnwJdxaY0Vw@mail.gmail.com>
I personally dis-trust "when" guards (which is why I didn't include
them in my reply). I have seen people over-use them and get clever
bugs because of it (a bit of the same way you were surprised with your
simpler bug), and they also prevent the compiler from being able to
tell whether a pattern-matching is exhaustive or has superfluous cases
-- because it does not try to guess the meaning of the boolean test. I
would use an if-then-else in your situation. That said, it is a matter
of taste, and many people will like your code as well.

Another thing I am sensitive to is that having "_" at the toplevel of
a pattern is bad form in my book. It does not matter much for
algebraic types that are known to have two cases (lists and options),
but whenever you are pattern-matching on a type that *you* defined and
that may get new constructors in the future, using "_" as a pattern
will prevent the compiler from warning you that this pattern-matching
need to be adapted (because it is not exhaustive anymore). Thus I try
to take the habit never to use "_" as a whole pattern (it is often fine
under a constructor), at any type (otherwise it's easy to slip back).
The version with "if" doesn't have a "_" as head pattern, so it is better
in that respect as well.

On Fri, Oct 9, 2015 at 2:48 PM, Seung-jin Kim [email protected]
[ocaml_beginners] <[email protected]> wrote:
> Sébastien and Gabriel;
>
> Thank you for your reply.
>
> Got it. My original goal was creating has_element function without if
> expression.
>
> And seems it is the one I was looking for;
>
> let rec has_element2 l e =
> match l with
> | [] -> false
> | h::t when h = e -> true
> | _ -> has_element2 (List.tl l) e
> ;;
>
>
> Thank you for all your help!
> Thanks to Hendrik as well :-)
>
> Regards,
>
> Seungjin
>
>
> On 10/09 09:49 AM, Sébastien Dailly [email protected] [ocaml_beginners] wrote:
>> Le 2015-10-09 05:55, Seung-jin Kim [email protected]
>> [ocaml_beginners] a écrit :
>> > 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.
>>
>> Hello,
>>
>> when you write
>>
>> > match l with
>> > | [] -> false
>> > | h::t -> …
>>
>> You do not match l with an existing variables named h and t. You create
>> two new ones which match the pattern.
>>
>> The same applies when you write :
>>
>> > match h with
>> > | e -> true
>>
>> You create a new variable name « e » which override the existing
>> variable. Of course this pattern always match.
>>
>> You can write :
>>
>> - either an if / else structure as in your first example.
>> - either a gard pattern in your pattern matching.
>>
>> Regards
>
> 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
>
>
>
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.