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 KAA05792; Thu, 3 Oct 2002 10:26:40 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA05805 for caml-list@pauillac.inria.fr; Thu, 3 Oct 2002 10:26:39 +0200 (MET DST) 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 KAA05584 for ; Thu, 3 Oct 2002 10:23:32 +0200 (MET DST) Received: from dpt-info.u-strasbg.fr (dpt-info.u-strasbg.fr [130.79.44.193]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g938NWD18434 for ; Thu, 3 Oct 2002 10:23:32 +0200 (MET DST) Received: from lambda.u-strasbg.fr (lambda.u-strasbg.fr [130.79.90.63]) by dpt-info.u-strasbg.fr (8.12.3/8.12.3/Debian -4) with ESMTP id g938NS0q029307; Thu, 3 Oct 2002 10:23:28 +0200 Received: from luther by lambda.u-strasbg.fr with local (Exim 3.36 #1 (Debian)) id 17x1OZ-0004Sl-00; Thu, 03 Oct 2002 10:31:43 +0200 Date: Thu, 3 Oct 2002 10:31:43 +0200 To: Alessandro Baretta Cc: Ocaml Subject: Re: [Caml-list] Pattern matching and strings Message-ID: <20021003083143.GA17152@lambda.u-strasbg.fr> References: <3D9AFED2.2050400@baretta.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3D9AFED2.2050400@baretta.com> User-Agent: Mutt/1.4i From: Sven Luther Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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. 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