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 NAA18692; Fri, 31 Jan 2003 13:25:14 +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 NAA18604 for ; Fri, 31 Jan 2003 13:25:12 +0100 (MET) Received: from beaune.inria.fr (beaune.inria.fr [128.93.8.3]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h0VCPBf16005; Fri, 31 Jan 2003 13:25:12 +0100 (MET) Received: by beaune.inria.fr (8.8.8/1.1.22.3/14Sep99-0328PM) id NAA0000025694; Fri, 31 Jan 2003 13:25:11 +0100 (MET) From: Luc Maranget Message-Id: <200301311225.NAA0000025694@beaune.inria.fr> Subject: Re: [Caml-list] Lexicographic sort To: lvibert@irisa.fr (Laurent Vibert) Date: Fri, 31 Jan 2003 13:25:11 +0100 (MET) Cc: francois.pottier@inria.fr (Francois Pottier), caml-list@inria.fr In-Reply-To: from "Laurent Vibert" at jan 31, 2003 11:33:34 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > > On Thu, 30 Jan 2003, Francois Pottier wrote: > > > > > Hi, > > > > I just found a nice way of writing lexicographic sort in O'Caml > > using Luc Maranget's recent extension of pattern syntax, which > > allows a single identifier to be bound in several alternatives. > > > > For instance, here is code that sorts integer triples: > > > > let compare (major1, middle1, minor1) (major2, middle2, minor2) = > > match major1 - major2, middle1 - middle2, minor1 - minor2 with > > | 0, 0, d > > | 0, d, _ > > | d, _, _ -> > > d > > > > Quite beautiful. Perhaps this is obvious to many, but I thought > > I'd post it. > > > > Warning, from the Caml manual, > http://pauillac.inria.fr/ocaml/htmlman/manual014.html > ``Or'' patterns : > If both matchings succeed, it is undefined which set of bindings is > selected > > eg. if (0,0,d) and (d,_,_) succeed, you don't hnow the result... > yes in this case, it works, but in some other, it doesn't : > let f = function (_::l) | l -> l > in f [1;2] > 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