On Fri, 16 Sep 2011 22:20:05 +0100
"Richard W.M. Jones" <[email protected]> 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: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mine, sont seulement les miennes} ***

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to