From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA03135; Thu, 26 Dec 2002 17:36:08 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA03826 for ; Thu, 26 Dec 2002 17:36:08 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id gBQGa7H07302; Thu, 26 Dec 2002 17:36:07 +0100 (MET) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA01979; Thu, 26 Dec 2002 17:36:06 +0100 (MET) From: Pierre Weis Message-Id: <200212261636.RAA01979@pauillac.inria.fr> Subject: Re: [Caml-list] Guards vs. conditionals In-Reply-To: <20021226091303.GA803@iliana> from Sven Luther at "Dec 26, 102 10:13:03 am" To: luther@dpt-info.u-strasbg.fr (Sven Luther) Date: Thu, 26 Dec 2002 17:36:06 +0100 (MET) Cc: caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk [...] > > 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