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 OAA24191; Fri, 4 Oct 2002 14:00:43 +0200 (MET DST) 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 OAA24209 for ; Fri, 4 Oct 2002 14:00:42 +0200 (MET DST) Received: from indyio.rz.uni-sb.de (indyio.rz.uni-saarland.de [134.96.7.3]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g94C0f510580 for ; Fri, 4 Oct 2002 14:00:41 +0200 (MET DST) Received: from mars.rz.uni-sb.de (IDENT:O8knztHDMqfhzHBPWMpEXWHHfC3Er4X+@mars.rz.uni-saarland.de [134.96.7.4]) by indyio.rz.uni-sb.de (8.9.3/8.9.3) with ESMTP id OAA19376430 for ; Fri, 4 Oct 2002 14:00:41 +0200 (CEST) Received: from uni-sb.de (uni-sb.de [134.96.7.230]) by mars.rz.uni-sb.de (8.8.8/8.8.4/8.8.2) with ESMTP id OAA219526033 for ; Fri, 4 Oct 2002 14:00:41 +0200 (CEST) Received: from cs.uni-sb.de (cs.uni-sb.de [134.96.252.31]) by uni-sb.de (8.12.4/2002060500) with ESMTP id g94C0eB02672 for ; Fri, 4 Oct 2002 14:00:40 +0200 (CEST) Received: from mail.cs.uni-sb.de (IDENT:nnvoBmnRrTt8gnzHtNNVKxYlIhKA3mMF@mail.cs.uni-sb.de [134.96.254.200]) by cs.uni-sb.de (8.12.1/2002091900) with ESMTP id g94C0dw17114 for ; Fri, 4 Oct 2002 14:00:39 +0200 (CEST) Received: from ps.uni-sb.de (grizzly.ps.uni-sb.de [134.96.186.68]) by mail.cs.uni-sb.de (8.12.5/2002072200) with ESMTP id g94C0cJ09723 for ; Fri, 4 Oct 2002 14:00:38 +0200 (CEST) X-Authentication-Warning: email: Host grizzly.ps.uni-sb.de [134.96.186.68] claimed to be ps.uni-sb.de Received: from ps.uni-sb.de (zoidberg.ps.uni-sb.de [134.96.186.121]) by ps.uni-sb.de (8.11.6/8.11.0) with ESMTP id g94C0bP14344; Fri, 4 Oct 2002 14:00:37 +0200 Message-ID: <3D9D82E5.31752861@ps.uni-sb.de> Date: Fri, 04 Oct 2002 14:00:37 +0200 From: Andreas Rossberg Organization: =?iso-8859-1?Q?Universit=E4t?= des Saarlandes X-Mailer: Mozilla 4.78 [en] (X11; U; Linux 2.4.9-21 i686) X-Accept-Language: de, en MIME-Version: 1.0 To: Ocaml Subject: Re: [Caml-list] Pattern matching and strings References: <3D9AFED2.2050400@baretta.com> <20021003083143.GA17152@lambda.u-strasbg.fr> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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. -- Andreas Rossberg, rossberg@ps.uni-sb.de "Computer games don't affect kids; I mean if Pac Man affected us as kids, we would all be running around in darkened rooms, munching magic pills, and listening to repetitive electronic music." - Kristian Wilson, Nintendo Inc. ------------------- 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