From: Philippe Veber <philippe.veber@gmail.com>
To: Raphael Proust <raphlalou@gmail.com>
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 00:18:47 +0200 [thread overview]
Message-ID: <CAOOOohR7sd7daYvggNMLZaUMXSifhJTkm8_dC4AuPb1+2xgv+g@mail.gmail.com> (raw)
In-Reply-To: <CAAmHUA=o8QWUKaLEJ4yPb1miGVNJ1b9pKqyyL3eVUqGJcEWQyA@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 3685 bytes --]
Thank you Raphael !
Indeed, keeping the C encoding underneath strings (and arrays) is a nice
property.
ph.
2011/9/14 Raphael Proust <raphlalou@gmail.com>
> Richard Jones described the internals of OCaml quite concisely. The
> difference between char arrays and strings is exposed in part two of
> his series of posts:
>
> https://rwmj.wordpress.com/2009/08/05/ocaml-internals-part-2-strings-and-other-types/
>
> There is a pointer to
>
> http://caml.inria.fr/pub/ml-archives/caml-list/2002/08/e109df224ff0150b302033e2002dbf87.en.html
> in the article.
>
> On 9/14/11, Philippe Veber <philippe.veber@gmail.com> wrote:
> > Hi Walter,
> >
> > Contrary to Prolog or Haskell, strings in ocaml are not represented as
> char
> > lists. They are exactly like char array, but have their own type,
> operations
> > and syntax : strings are created with String.make (similar to
> Array.make),
> > their length is given by String.length (sim. to Array.length) and the
> chars
> > are accessed with the notation s.[i] (similar to t.(i)). Actually I don't
> > know why they are not defined like char arrays (anyone on this ?). Long
> > story short, recursive formulations on strings (likewise for array) will
> > often rely on indices (and thus, not much on pattern matching). Note that
> > you can use optional arguments to hide indices :
> >
> > let rec iter f ?(k = 0) s =
> > if k < String.length s then (
> > f s.[k] ;
> > iter f ~k:(k + 1) s
> > )
> >
> > let _ = iter print_char "abc";;
> >
> >
> > The closest to your request I see can be achieved using ocaml batteries
> (*),
> > by transforming your string into a list:
> >
> > let rec iter_aux f = function
> > [] -> ()
> > | c :: s1 -> f c ; iter_aux f s1
> > let iter f s = iter_aux f (String.explode s)
> >
> > But this won't be very efficient !
> >
> > You won't find advanced string pattern matching in core ocaml. But
> micmatch
> > seems a nice way to go if that's what you're looking for.
> >
> > cheers,
> > Philippe.
> >
> > (*) http://batteries.forge.ocamlcore.org/
> >
> >
> > 2011/9/14 Walter Cazzola <cazzola@dico.unimi.it>
> >
> >> Hi all,
> >> I'm just trying to write a recursive function that iterates¹ on a string
> >> and I'd like to use pattern matching as in:
> >>
> >> let rec iter f s =
> >> match s with
> >> | "" -> unit;
> >> | c^s1 -> f c; iter f s1;;
> >>
> >> but the ^ concatenates 2 strings and not a char with a string and above
> >> all seems to be inadmissible in the patterns.
> >>
> >> Does this mean that I can't write a function on strings by pattern
> >> matching or is there something I don't know?²
> >>
> >> Thanks for the help
> >> Walter
> >>
> >> ¹ I know that exists String.iter but I'd like to improve my skill in
> >> writing functions by using pattern matching
> >> ² I read about micmatch but I'd like to avoid non standard packages.
> >> --
> >> --
> >> Caml-list mailing list. Subscription management and archives:
> >> https://sympa-roc.inria.fr/**wws/info/caml-list<
> https://sympa-roc.inria.fr/wws/info/caml-list>
> >> Beginner's list:
> >> http://groups.yahoo.com/group/**ocaml_beginners<
> http://groups.yahoo.com/group/ocaml_beginners>
> >> Bug reports:
> >> http://caml.inria.fr/bin/caml-**bugs<http://caml.inria.fr/bin/caml-bugs
> >
> >>
> >>
> >
> > --
> > 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
> >
> >
>
>
> --
> _______
> Raphael
>
[-- Attachment #2: Type: text/html, Size: 5744 bytes --]
next prev parent reply other threads:[~2011-09-16 22:19 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
2011-09-14 20:57 ` Philippe Veber
2011-09-14 21:44 ` Raphael Proust
2011-09-16 22:18 ` Philippe Veber [this message]
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=CAOOOohR7sd7daYvggNMLZaUMXSifhJTkm8_dC4AuPb1+2xgv+g@mail.gmail.com \
--to=philippe.veber@gmail.com \
--cc=caml-list@inria.fr \
--cc=cazzola@dico.unimi.it \
--cc=raphlalou@gmail.com \
/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;
as well as URLs for NNTP newsgroup(s).