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 VAA08698; Fri, 4 Oct 2002 21:07:48 +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 VAA08490 for ; Fri, 4 Oct 2002 21:07:47 +0200 (MET DST) Received: from mel-rto2.wanadoo.fr (smtp-out-2.wanadoo.fr [193.252.19.254]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g94J7k506787 for ; Fri, 4 Oct 2002 21:07:46 +0200 (MET DST) Received: from mel-rta10.wanadoo.fr (193.252.19.193) by mel-rto2.wanadoo.fr (6.5.007) id 3D89D999008B855D; Fri, 4 Oct 2002 21:03:47 +0200 Received: from iliana (80.13.114.140) by mel-rta10.wanadoo.fr (6.5.007) id 3D80120800DAB7D0; Fri, 4 Oct 2002 21:03:47 +0200 Received: from luther by iliana with local (Exim 3.36 #1 (Debian)) id 17xXsl-0000Mv-00; Fri, 04 Oct 2002 21:13:03 +0200 Date: Fri, 4 Oct 2002 21:13:03 +0200 To: Andreas Rossberg Cc: Ocaml Subject: Re: [Caml-list] Pattern matching and strings Message-ID: <20021004191303.GA1404@iliana> References: <3D9AFED2.2050400@baretta.com> <20021003083143.GA17152@lambda.u-strasbg.fr> <3D9D82E5.31752861@ps.uni-sb.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3D9D82E5.31752861@ps.uni-sb.de> User-Agent: Mutt/1.4i From: Sven LUTHER Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Fri, Oct 04, 2002 at 02:00:37PM +0200, Andreas Rossberg wrote: > Sven Luther wrote: > > > > On Wed, Oct 02, 2002 at 04:12:34PM +0200, Alessandro Baretta wrote: > > > 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 > > > > What about : > > > > ... = function > > | str when String.sub str 0 3 = "foo" -> foo (String.sub str 2 (String.length str - 3)) > > | str when String.sub str 0 3 = "bar" -> bar (String.sub str 2 (String.length str - 3)) > > | _ -> raise Unrecognized > > > > Sure, this code is not very optimal, i guess you could write a nicer > > function which will test the string incrementally using just String.get > > or something such, but i suppose it will do the thing you want. > > The SML basis library provides the nice but often overlooked concept of > substrings (or more generally, vector slices, > http://SML.sourceforge.net/Basis/substring.html). Ported to Caml the > Substring module would enable > > > ... = function > > | s when Substring.is_prefix "foo" s -> foo (Substring.triml 3 s) > > | s when Substring.is_prefix "bar" s -> bar (Substring.triml 3 s) > > | _ -> raise Unrecognized > > where your parsing functions operate on substrings instead of strings. > Such an approach seems like a good compromise between use of a heavy > regexp lib and inefficient repeated copying of parts of strings. Well, like i said, it was just a quick implementation, but i am sure you can do this more or less optimaly with a hand coded String.get using comparison function. I don't know if you really can escape the use of the final copying of the string though. Friendly, Sven Luther ------------------- 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