Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Guards vs. conditionals
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <pierre.weis@i...>
Subject: Re: [Caml-list] Guards vs. conditionals
[...]
> > 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