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 RAA00537; Fri, 4 Oct 2002 17:14:32 +0200 (MET DST) 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 RAA00016 for ; Fri, 4 Oct 2002 17:14:31 +0200 (MET DST) 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 g94FEV527481 for ; Fri, 4 Oct 2002 17:14:31 +0200 (MET DST) Received: by beaune.inria.fr (8.8.8/1.1.22.3/14Sep99-0328PM) id RAA0000017577; Fri, 4 Oct 2002 17:14:30 +0200 (MET DST) From: Luc Maranget Message-Id: <200210041514.RAA0000017577@beaune.inria.fr> Subject: Re: [Caml-list] Pattern matching and strings To: rossberg@ps.uni-sb.de (Andreas Rossberg) Date: Fri, 4 Oct 2002 17:14:30 +0200 (MET DST) Cc: caml-list@inria.fr (Ocaml) In-Reply-To: <3D9D82E5.31752861@ps.uni-sb.de> from "Andreas Rossberg" at oct 04, 2002 02:00:37 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > I have to do a little bit of pattern matching on strings. My > first instict was to write something like the following. > > let foo x = ... > let bar x = ... > ... = function > | "foo" ^ rest -> foo rest > | "bar" ^ rest -> bar rest > | _ -> raise Unrecognized I have thought about that a little. My first guess is that such matching should be distinct from ordinary PM, mixing the two would be an implementor nightmare. My second guess is that what you want is regexp matching + a construct for binding subparts of the matched string (maybe I am wrong here). Using ocamllex syntax for patterns (+ as) your exemple could be written regexpmatch s with | "foo" (_* as x) -> foo x | "bar" (_* as x) -> bar x | _* -> raise Unrecognized This would be much nicer than using various regexp packages API, the real add-on being the variables in place of \1, \2 etc. Of course this would work only in the case where all patterns are known statically and implementation is not 100% trivial, if you want some warnings and compile-time production of matching automata. (ie if you do not rely on regexp package). In fact such, an extension is probably feasible using camlp4 and this would probably be the best solution, to avoid extra-complexity in the compiler itself. In the end, do not hold your breath. I won't probably do that. But it can be a interesting project for a compiler course... --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