From: Basile Starynkevitch <basile@starynkevitch.net>
To: "Richard W.M. Jones" <rich@annexia.org>
Cc: Walter Cazzola <cazzola@dico.unimi.it>,
OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] pattern matching on strings
Date: Sat, 17 Sep 2011 10:15:52 +0200 [thread overview]
Message-ID: <20110917101552.c9c0661c2b6d2992e55491ba@starynkevitch.net> (raw)
In-Reply-To: <20110916212005.GA22906@annexia.org>
On Fri, 16 Sep 2011 22:20:05 +0100
"Richard W.M. Jones" <rich@annexia.org> 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} ***
next prev parent reply other threads:[~2011-09-17 8:16 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-09-14 20:16 Walter Cazzola
2011-09-14 20:40 ` Basile Starynkevitch
2011-09-16 21:20 ` Richard W.M. Jones
2011-09-17 8:15 ` Basile Starynkevitch [this message]
2011-09-14 20:57 ` Philippe Veber
2011-09-14 21:44 ` Raphael Proust
2011-09-16 22:18 ` Philippe Veber
2011-09-14 21:14 ` Philippe Strauss
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110917101552.c9c0661c2b6d2992e55491ba@starynkevitch.net \
--to=basile@starynkevitch.net \
--cc=caml-list@inria.fr \
--cc=cazzola@dico.unimi.it \
--cc=rich@annexia.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox