From mboxrd@z Thu Jan 1 00:00:00 1970 X-Sympa-To: caml-list@inria.fr 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 p8GMJDUK022835 for ; Sat, 17 Sep 2011 00:19:13 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AiQDAHvKc07RVdQxkGdsb2JhbABBmHE1hxEBhyAIFAEBAQEJCQ0HFAQhgVMBAQEBAgESAhMZARsSCwEDAQsGBQsDCg0hIQEBEQEFAQoSBhMSEIdVBJdVCotBglqFGTuIbQIDBoZyBIJUkHaKEoJvPYNw X-IronPort-AV: E=Sophos;i="4.68,395,1312149600"; d="scan'208";a="120176264" Received: from mail-vw0-f49.google.com ([209.85.212.49]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 17 Sep 2011 00:19:08 +0200 Received: by vws8 with SMTP id 8so7238077vws.36 for ; Fri, 16 Sep 2011 15:19:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=cS+QyTKyBiLMIOYXllsq4iRWUcwiXGmNv5Vti3X5qfU=; b=NMTn4NcVAM5SOAl3nR26cb+pVyZrwFVJH9qcMqqDcf10VvogTGNuaELVpdCFDhb+rb mYnZcMd34lgX6SqqQ6sAKgpysK8iTSV1BjWGcqptHppNVVh1R2vfQh5L2yMnBZZupLez aGkVcrAIV/yvzy98GApJgNZ5yMeJaKPr7hgkc= Received: by 10.52.29.145 with SMTP id k17mr109834vdh.455.1316211547074; Fri, 16 Sep 2011 15:19:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.221.13.202 with HTTP; Fri, 16 Sep 2011 15:18:47 -0700 (PDT) In-Reply-To: References: From: Philippe Veber Date: Sat, 17 Sep 2011 00:18:47 +0200 Message-ID: To: Raphael Proust Cc: Walter Cazzola , OCaML Mailing List Content-Type: multipart/alternative; boundary=20cf307f3484b66ecb04ad166172 X-Validation-by: philippe.veber@gmail.com Subject: Re: [Caml-list] pattern matching on strings --20cf307f3484b66ecb04ad166172 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thank you Raphael ! Indeed, keeping the C encoding underneath strings (and arrays) is a nice property. ph. 2011/9/14 Raphael Proust > 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/e109df224ff0150b30= 2033e2002dbf87.en.html > in the article. > > On 9/14/11, Philippe Veber 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 th= at > > you can use optional arguments to hide indices : > > > > let rec iter f ?(k =3D 0) s =3D > > if k < String.length s then ( > > f s.[k] ; > > iter f ~k:(k + 1) s > > ) > > > > let _ =3D 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 =3D function > > [] -> () > > | c :: s1 -> f c ; iter_aux f s1 > > let iter f s =3D 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 > > > >> Hi all, > >> I'm just trying to write a recursive function that iterates=B9 on a st= ring > >> and I'd like to use pattern matching as in: > >> > >> let rec iter f s =3D > >> 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?=B2 > >> > >> Thanks for the help > >> Walter > >> > >> =B9 I know that exists String.iter but I'd like to improve my skill in > >> writing functions by using pattern matching > >> =B2 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 > > >> > >> > > > > -- > > 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 > --20cf307f3484b66ecb04ad166172 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thank you Raphael !
Indeed, keeping the C encoding underneath strings (a= nd arrays) is a nice property.
ph.

201= 1/9/14 Raphael Proust <raphlalou@gmail.com>
Richard Jones des= cribed 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, oper= ations
> and syntax : strings are created with String.make (similar to Array.ma= ke),
> 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 do= n't
> know why they are not defined like char arrays (anyone on this ?). Lon= g
> story short, recursive formulations on strings (likewise for array) wi= ll
> often rely on indices (and thus, not much on pattern matching). Note t= hat
> you can use optional arguments to hide indices :
>
> let rec iter f ?(k =3D 0) s =3D
> =A0 if k < String.length s then (
> =A0 =A0 f s.[k] ;
> =A0 =A0 iter f ~k:(k + 1) s
> =A0 )
>
> let _ =3D iter print_char "abc";;
>
>
> The closest to your request I see can be achieved using ocaml batterie= s (*),
> by transforming your string into a list:
>
> let rec iter_aux f =3D function
> =A0 =A0 [] -> ()
> =A0 | c :: s1 -> f c ; iter_aux f s1
> let iter f s =3D 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=B9= on a string
>> and I'd like to use pattern matching as in:
>>
>> let rec iter f s =3D
>> =A0match s with
>> =A0 =A0 | "" =A0-> unit;
>> =A0 =A0 | 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 pat= tern
>> matching or is there something I don't know?=B2
>>
>> Thanks for the help
>> Walter
>>
>> =B9 I know that exists String.iter but I'd like to improve my = skill in
>> =A0writing functions by using pattern matching
>> =B2 I read about micmatch but I'd like to avoid non standard p= ackages.
>> --
>> --
>> Caml-list mailing list. =A0Subscription 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&g= t;
>>
>>
>
> --
> Caml-list mailing list. =A0Subscript= ion 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

--20cf307f3484b66ecb04ad166172--