<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/01/25dd585393832a00ec069df6b9aa3e64"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-01-02T09:54:24"
  subject="Re: [Caml-list] Guards vs. conditionals"
  prev="2003/01/f09c1cd83ec921dc87b5dc72261e2890"
  next="2003/01/9c6de8029efabdd5e50e038b39b60afe"
  prev-in-thread="2002/12/955553377ddd7b841cae02e31027af85"
  next-in-thread="2002/12/7ece4f35fb5ba3db3c8c56d3295a92ef"
  prev-thread="2002/12/d8e6906dcdf57f1da094ffdf57d05c60"
  next-thread="2002/12/20995c2eed42f26e0a7193d54f646852"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Guards vs. conditionals">
<msg 
  url="2002/12/955553377ddd7b841cae02e31027af85"
  from="Matt Gushee &lt;mgushee@h...&gt;"
  author="Matt Gushee"
  date="2002-12-26T07:06:59"
  subject="[Caml-list] Guards vs. conditionals">
<msg 
  url="2003/01/25dd585393832a00ec069df6b9aa3e64"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-01-02T09:54:24"
  subject="Re: [Caml-list] Guards vs. conditionals">
</msg>
<msg 
  url="2002/12/7ece4f35fb5ba3db3c8c56d3295a92ef"
  from="Sven Luther &lt;luther@d...&gt;"
  author="Sven Luther"
  date="2002-12-26T09:13:05"
  subject="Re: [Caml-list] Guards vs. conditionals">
<msg 
  url="2002/12/a1c92ae87343785a0ac79d977ebaaeff"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2002-12-26T16:36:08"
  subject="Re: [Caml-list] Guards vs. conditionals">
</msg>
</msg>
</msg>
</thread>

<contents>
&gt; I'm just curious about something. I've been noticing that in many cases
&gt; (if not always) a pattern match with guard expressions in OCaml is
&gt; equivalent to a conditional statement.

Not always strictly equivalent.  Consider

  match x with hd::tl when hd &lt;&gt; 0 -&gt; &lt;expr1&gt;
             | _ -&gt; &lt;expr2&gt;

Without "when", you'd need to duplicate &lt;expr2&gt;:

  match x with hd::tl -&gt; if hd &lt;&gt; 0 then &lt;expr1&gt; else &lt;expr2&gt;
             | _ -&gt; &lt;expr2&gt;

&gt; So, my question is, is there any objective reason to prefer the
&gt; pattern-match version over the conditional, or vice versa? Or is it just
&gt; a matter of coding style?

It's just a question of style.  Personally, I'd recommend using "when"
in cases where "real" pattern-matching is involved, as in the example
above, and cascades of "if...then...else..." in cases where it doesn't
cause duplication of code.  But you can opt for a different style.

- Xavier Leroy
-------------------
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

</contents>

</message>

