<?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/5ef42def11e5adc1e52c1ccc74ec1d0b"
  from="Luc Maranget &lt;luc.maranget@i...&gt;"
  author="Luc Maranget"
  date="2003-01-31T12:25:14"
  subject="Re: [Caml-list] Lexicographic sort"
  prev="2003/01/f37d898ed760057b2a30d07937bc9aae"
  next="2003/01/a9673766c5e0333ea0c657cce6e6a1c6"
  prev-thread="2003/01/2b94ee440d88d35bc171415731c354a7"
  next-thread="2003/01/ca1dffd1660ca65a4b05e25358542cd4"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="Re: [Caml-list] Lexicographic sort">
<msg 
  url="2003/01/5ef42def11e5adc1e52c1ccc74ec1d0b"
  from="Luc Maranget &lt;luc.maranget@i...&gt;"
  author="Luc Maranget"
  date="2003-01-31T12:25:14"
  subject="Re: [Caml-list] Lexicographic sort">
</msg>
</thread>

<contents>
&gt; 
&gt; On Thu, 30 Jan 2003, Francois Pottier wrote:
&gt; 
&gt; &gt; 
&gt; &gt; Hi,
&gt; &gt; 
&gt; &gt; I just found a nice way of writing lexicographic sort in O'Caml
&gt; &gt; using Luc Maranget's recent extension of pattern syntax, which
&gt; &gt; allows a single identifier to be bound in several alternatives.
&gt; &gt; 
&gt; &gt; For instance, here is code that sorts integer triples:
&gt; &gt; 
&gt; &gt;   let compare (major1, middle1, minor1) (major2, middle2, minor2) =
&gt; &gt;     match major1 - major2, middle1 - middle2, minor1 - minor2 with
&gt; &gt;     | 0, 0, d
&gt; &gt;     | 0, d, _
&gt; &gt;     | d, _, _ -&gt;
&gt; &gt; 	d
&gt; &gt; 
&gt; &gt; Quite beautiful. Perhaps this is obvious to many, but I thought
&gt; &gt; I'd post it.
&gt; &gt; 
&gt; 
&gt; Warning, from the Caml manual,
&gt; http://pauillac.inria.fr/ocaml/htmlman/manual014.html
&gt; ``Or'' patterns :
&gt; If both matchings succeed, it is undefined which set of bindings is 
&gt; selected
&gt; 
&gt; eg. if (0,0,d) and (d,_,_) succeed, you don't hnow the result...
&gt; yes in this case, it works, but in some other, it doesn't :
&gt; let f = function (_::l) | l -&gt; l
&gt; in f [1;2] 
&gt; 


Well, Francois's example is a good incentive to suppress this
undefined matching order.

In fact, matching is left-to-right, except when one of the
alternatives is a variable (or an underscore).  I have a loose agenda
to enforce left-to-right matching order, and Francois's code won't be
broken by future versions.


Again this undefined matching order in or-patterns is a mistake of mine
and it should be corrected.


--Luc



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

