caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <pierre.weis@inria.fr>
To: luther@dpt-info.u-strasbg.fr (Sven Luther)
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Guards vs. conditionals
Date: Thu, 26 Dec 2002 17:36:06 +0100 (MET)	[thread overview]
Message-ID: <200212261636.RAA01979@pauillac.inria.fr> (raw)
In-Reply-To: <20021226091303.GA803@iliana> from Sven Luther at "Dec 26, 102 10:13:03 am"

[...]
> > So, my question is, is there any objective reason to prefer the
> > pattern-match version over the conditional, or vice versa? Or is it just
> > a matter of coding style?
> 
> It is just a matter of coding style. I think that the if version is
> maybe easier to do prooves on or something such,
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What do you mean ?

> and that guard version is easier to read and maybe better when there
> are more than one condition, but the compiler does not know when the
> guards cover all the cases, and may output a warning when non is
> needed :
> 
> consider :
> 
> let foo = function
> | i when i > 500 -> 1
> | i when i = 500 -> 2
> | i when i < 500 -> 3
> 
> Which will output a warning.
> 
> Friendly,
> 
> Sven Luther

The guard version has the additional good property of allowing
sequences in the clauses with no need for parens or begin end:

 | i when i > 500 -> print i; printe_newline ()
 | i -> ...

Concerning the conjunction of guards and partial matches, the problem
is covered in large in the FAQ of the language, under the question

* Partial match with guards ?

Have a look to:

http://pauillac.inria.fr/caml/FAQ/FAQ_EXPERT-eng.html#gardes_partielles

In short, you just have to remember that the last clause does not need
a guard: use comments and the warning desapears :). For instance,

 | i (* when i < 500 *) -> 3

Friendly,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2002-12-26 16:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-26  7:06 Matt Gushee
2002-12-26  9:13 ` Sven Luther
2002-12-26 16:36   ` Pierre Weis [this message]
2003-01-02  9:54 ` Xavier Leroy
     [not found] <D4DBD8568F05D511A1C20002A55C008C11AC0404@uswaumsx03medge.med.ge.com>
2002-12-27  7:37 ` Matt Gushee
2002-12-27  9:15   ` Luc Maranget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200212261636.RAA01979@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=luther@dpt-info.u-strasbg.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).