From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p8H8G6aV003663 for ; Sat, 17 Sep 2011 10:16:06 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av0EAPBVdE7DDgCO/2dsb2JhbABBp2B4gVMBAQQBJxMcIwULCxguOR4GE4d3ArNThngEnVuHGw X-IronPort-AV: E=Sophos;i="4.68,397,1312149600"; d="scan'208";a="120203064" Received: from 195-14-0-142.nuxit.net (HELO de558.ispfr.net) ([195.14.0.142]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/AES256-SHA; 17 Sep 2011 10:16:01 +0200 Received: from ours.starynkevitch.net ([213.41.244.95] helo=glinka.lesours) by de558.ispfr.net with smtp (Exim 4.72) (envelope-from ) id 1R4q47-0008U4-Jb; Sat, 17 Sep 2011 10:15:59 +0200 Date: Sat, 17 Sep 2011 10:15:52 +0200 From: Basile Starynkevitch To: "Richard W.M. Jones" Cc: Walter Cazzola , OCaML Mailing List Message-Id: <20110917101552.c9c0661c2b6d2992e55491ba@starynkevitch.net> In-Reply-To: <20110916212005.GA22906@annexia.org> References: <20110914224011.70c49bb251e84a4186a2b44d@starynkevitch.net> <20110916212005.GA22906@annexia.org> X-Mailer: Sylpheed 3.2.0beta3 (GTK+ 2.24.6; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] pattern matching on strings On Fri, 16 Sep 2011 22:20:05 +0100 "Richard W.M. Jones" wrote: > On Wed, Sep 14, 2011 at 10:40:11PM +0200, Basile Starynkevitch wrote: > > And there is a reason why you cannot match (in Ocaml) on the content > > of strings (or arrays). It won't be easy to implement efficiently > > (you would need to copy a substring or subarray when matching) > > How about just prefix matching? That on its own would be very useful. > > For example in web app that was passed arguments foo_1, foo_2, bar_1, > bar_5 you could parse the arguments like this: > > match arg with > | "foo_" ^ s -> (* ..code_foo.. *) > | "bar_" ^ s -> (* ..code_bar.. *) [[NB I edited Richard's examples' comments]] If the pattern variable s is indeed used in code_foo or code_bar you need to copy a substring of arg, don't you? And we could do that (less efficiently) with something like match arg with | ss when string_starts_with ss "foo_" -> let s = rest_of_string ss "foo_" in (* ..code_foo.. *) | ss when string_starts_with ss "bar_" -> let s = rest_of_string ss "bar_" in (* ..code_foo.. *) [the code of string_starts_with & rest_of_string is obvious] Actually, a syntactic camlp4 trick could do the above However, what would be great would be to be able to code match arg with | "beef_" ^ s -> beef_case s | "gee_" ^ t -> gee_case t and have the generated code factorize the common sub-test, that is that arg.[1] is 'e' and arg.[2] is 'e' in both cases. We don't have that, and we can't do that with syntactic preprocessing. Cheers. -- Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basilestarynkevitchnet mobile: +33 6 8501 2359 8, rue de la Faiencerie, 92340 Bourg La Reine, France *** opinions {are only mine, sont seulement les miennes} ***