<?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="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"
  prev="2002/12/4c60252b35d6717312ae7d96ccb279d8"
  next="2002/12/0763ea0977ac7155c8b4eea75ee0f625"
  next-in-thread="2003/01/25dd585393832a00ec069df6b9aa3e64"
  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>
Hi, all--

I'm just curious about something. I've been noticing that in many cases
(if not always) a pattern match with guard expressions in OCaml is
equivalent to a conditional statement. E.g.:

  let foo =
    function
    | i when i &gt;= 500 -&gt; true
    | i -&gt; false
    
does the same thing as

  let foo i =
    if i &gt;= 500 then true
    else false

And I wrote a pair of small programs, each of which invokes one of these
functions on a random integer 100,000 times. The executables, whether
byte-compiled or native, are almost exactly the same size and, according
to a round of informal tests with GNU time, run at the same speed. This
suggests that the compiled code is for all practical purposes the same.

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?
        
-- 
Matt Gushee                 When a nation follows the Way,
Englewood, Colorado, USA    Horses bear manure through
mgushee@havenrock.com           its fields;
http://www.havenrock.com/   When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                                
                            --Lao Tzu (Peter Merel, trans.)
-------------------
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>

