From: Sven LUTHER <luther@dpt-info.u-strasbg.fr>
To: Andreas Rossberg <rossberg@ps.uni-sb.de>
Cc: Ocaml <caml-list@inria.fr>
Subject: Re: [Caml-list] Pattern matching and strings
Date: Fri, 4 Oct 2002 21:13:03 +0200 [thread overview]
Message-ID: <20021004191303.GA1404@iliana> (raw)
In-Reply-To: <3D9D82E5.31752861@ps.uni-sb.de>
On Fri, Oct 04, 2002 at 02:00:37PM +0200, Andreas Rossberg wrote:
> 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.
Well, like i said, it was just a quick implementation, but i am sure you
can do this more or less optimaly with a hand coded String.get using
comparison function. I don't know if you really can escape the use of
the final copying of the string though.
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
next prev parent reply other threads:[~2002-10-04 19:07 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-10-02 14:12 Alessandro Baretta
2002-10-02 15:02 ` Luc Maranget
2002-10-02 16:21 ` [Caml-list] Pattern matching and strings (and a mini-bug in Scanf) Alessandro Baretta
2002-10-02 17:15 ` Luc Maranget
2002-10-02 18:30 ` Alessandro Baretta
2002-10-02 18:32 ` Benjamin C. Pierce
2002-10-04 13:01 ` Florian Douetteau
2002-10-04 9:07 ` Pierre Weis
2002-10-04 9:31 ` Alessandro Baretta
2002-10-04 10:03 ` Pierre Weis
2002-10-04 10:23 ` Alessandro Baretta
2002-10-04 12:11 ` Remi VANICAT
2002-10-04 12:40 ` [Caml-list] Ocaml 3.06 Win2K OCAML/C link problem Ward Wheeler
2002-10-04 13:12 ` [Caml-list] Pattern matching and strings (and a mini-bug inScanf) Frederic van der Plancke
2002-10-04 19:31 ` [Caml-list] Pattern matching and strings (and a mini-bug in Scanf) Alessandro Baretta
2002-10-07 7:17 ` Pierre Weis
2002-10-02 20:59 ` [Caml-list] views for strings (was: Pattern matching and strings) Chris Hecker
2002-10-02 23:09 ` [Caml-list] " Alessandro Baretta
2002-10-03 8:31 ` [Caml-list] Pattern matching and strings Sven Luther
2002-10-04 12:00 ` Andreas Rossberg
2002-10-04 14:21 ` Kontra, Gergely
2002-10-04 15:14 ` Luc Maranget
2002-10-04 19:38 ` Alessandro Baretta
2002-10-05 6:34 ` [Caml-list] Camlp4 (Was: Pattern matching and strings) Daniel de Rauglaudre
2002-10-05 12:47 ` Sven LUTHER
2002-10-05 12:42 ` Daniel de Rauglaudre
2002-10-05 13:41 ` Michel Mauny
2002-10-05 13:47 ` Daniel de Rauglaudre
2002-10-05 14:09 ` Michel Mauny
2002-10-05 18:13 ` Alessandro Baretta
2002-10-05 20:30 ` [Caml-list] Future of Camlp4 Daniel de Rauglaudre
2002-10-04 19:13 ` Sven LUTHER [this message]
[not found] <200210070802.KAA0000024668@beaune.inria.fr>
2002-10-07 16:15 ` [Caml-list] Pattern matching and strings Alessandro Baretta
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=20021004191303.GA1404@iliana \
--to=luther@dpt-info.u-strasbg.fr \
--cc=caml-list@inria.fr \
--cc=rossberg@ps.uni-sb.de \
/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