From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id C662ABB9C for ; Wed, 23 Nov 2005 23:25:58 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jANMPv2x011699 for ; Wed, 23 Nov 2005 23:25:58 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA12165 for ; Wed, 23 Nov 2005 23:25:57 +0100 (MET) Received: from out4.smtp.messagingengine.com (out4.smtp.messagingengine.com [66.111.4.28]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jANMPuWV024326; Wed, 23 Nov 2005 23:25:56 +0100 Received: from frontend1.internal (mysql-sessions.internal [10.202.2.149]) by frontend1.messagingengine.com (Postfix) with ESMTP id BD679D179DB; Wed, 23 Nov 2005 17:25:54 -0500 (EST) Received: from frontend2.messagingengine.com ([10.202.2.151]) by frontend1.internal (MEProxy); Wed, 23 Nov 2005 17:25:54 -0500 X-Sasl-enc: S7inxF3cj+YaOFiwULH/QJ7+e3SV1SN+cgQHEFdB1i2I 1132784753 Received: from munge (burnham.ljcrf.edu [192.231.106.2]) by frontend2.messagingengine.com (Postfix) with ESMTP id 3746857146E; Wed, 23 Nov 2005 17:25:51 -0500 (EST) Date: Wed, 23 Nov 2005 14:25:40 -0800 (PST) From: Martin Jambon X-X-Sender: martin@munge To: skaller Cc: Martin Jambon , Luc Maranget , Christophe Raffalli , sejourne_kevin , caml-list Subject: Re: [Caml-list] Request for complete pattern matching In-Reply-To: <1132781404.25948.78.camel@rosella> Message-ID: References: <43839F1A.2080909@univ-savoie.fr> <43842069.3070700@yahoo.fr> <43848103.9060504@univ-savoie.fr> <20051123183134.GB6446@yquem.inria.fr> <1132781404.25948.78.camel@rosella> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at concorde with ID 4384EC75.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 4384EC74.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 endline:01 endline:01 regexps:01 ocaml:01 regexps:01 'when':01 bindings:01 defines:01 val:01 4010:98 wrote:01 wrote:01 matched:01 functions:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.3 On Thu, 24 Nov 2005, skaller wrote: > On Wed, 2005-11-23 at 12:56 -0800, Martin Jambon wrote: > >> (* toto.ml *) >> try >> while true do >> match read_line () with >> / upper / | / _* "." eos / -> print_endline "looks like a sentence" >> | "." | / ("bye"~ space*)+ / -> print_endline "Bye!"; exit 0 >> | _ -> print_endline "???" >> done >> with End_of_file -> () >> >> Notes: >> - the stuff between slashes are regexps >> - "." and the last _ are regular OCaml patterns >> - regexps are replaced by an identifier which is matched after the >> arrow using library functions, then it is decided whether to jump to the >> next case or to execute the user-given expression. > > Why can't this be done with 'when' clause? > > EG: > > | ... predicate ... -> ... > > replace by > > | ... variable .. when predicate(variable) It was not shown in my example, but the predicate can introduce bindings: | String (predicate which also defines x) | Int x -> ... | _ -> ... You can do this: # let f = function `Int n | `String / (int as n : int) eos / -> 2 * n | _ -> -1;; val f : _[> `Int of int | `String of string ] -> int = # f (`Int 123);; - : int = 246 # f (`String "2005");; - : int = 4010 # f (`String "x");; - : int = -1 > This would seem to fail only in that 'predicate' code > cannot refer to another predicate, since the variable > name is not known. > > The 'break' implementation, however, may be overtly > more efficient (save duplicating predicate evaluations). > > -- > John Skaller > Felix, successor to C++: http://felix.sf.net > > -- Martin Jambon, PhD http://martin.jambon.free.fr Store and share your bioinformatics tips at http://wikiomics.org