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 p8EKvwFR004014 for ; Wed, 14 Sep 2011 22:57:58 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqsBANMUcU7RVdivkGdsb2JhbABCmFw1hw0BhyEIFAEBAQEJCQ0HFAQigVMBAQQBEgITGQEbEgsBAwELBgULGiEiAREBBQEKEgYTEhCHVQSYPQqLQIJXhVM7iG0CAwaGaASCVJBrjHo9gkSBLA X-IronPort-AV: E=Sophos;i="4.68,382,1312149600"; d="scan'208";a="119857493" Received: from mail-qy0-f175.google.com ([209.85.216.175]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 14 Sep 2011 22:57:37 +0200 Received: by qyk10 with SMTP id 10so4712325qyk.6 for ; Wed, 14 Sep 2011 13:57:36 -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=nMQVDW2cY0K4tcz63ADRGX5GQ+WZs0C872wpIxgRYi8=; b=dPFx6q5wO4P076yxQ5DkZacjuSfalc+tWYu29vkOnaVTjkZdYrBeJz2DotX0Bi9q8o x/DdSLHvEDWpf4aTehAULHZVf8UOAUVRIyLKAR8bUQqvfvsRcDyehso/kv73Xmpy3Vak uvzz8jqMVYMbc/po4IeTKS9O4Az89w5oD4AT8= Received: by 10.52.101.227 with SMTP id fj3mr292815vdb.367.1316033856233; Wed, 14 Sep 2011 13:57:36 -0700 (PDT) MIME-Version: 1.0 Received: by 10.221.13.202 with HTTP; Wed, 14 Sep 2011 13:57:16 -0700 (PDT) In-Reply-To: References: From: Philippe Veber Date: Wed, 14 Sep 2011 22:57:16 +0200 Message-ID: To: Walter Cazzola Cc: OCaML Mailing List Content-Type: multipart/alternative; boundary=bcaec5485c94835f4804aced02d0 X-Validation-by: philippe.veber@gmail.com Subject: Re: [Caml-list] pattern matching on strings --bcaec5485c94835f4804aced02d0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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 =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 string > 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 > Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-**bugs > > --bcaec5485c94835f4804aced02d0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi Walter,

Contrary to Prolog or Haskell, strings in ocaml are not r= epresented as char lists. They are exactly like char array, but have their = own type, operations and syntax : strings are created with String.make (sim= ilar 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 (an= yone on this ?). Long story short, recursive formulations on strings (likew= ise for array) will often rely on indices (and thus, not much on pattern ma= tching). Note that 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=A0 f s.[k] ;
=A0=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 batte= ries (*), by transforming your string into a list:

let rec iter_aux f =3D function =A0=A0=A0 [] -> ()
=A0 | c :: s1 -> f c ; iter_aux f s1
let iter f s =3D iter_a= ux 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 l= ooking for.

cheers,
Philippe.

(*) http://batteries.forge.ocamlcore.org/


2011/9/14 Walter Cazzola <cazzola@dico.unimi.it&g= t;
Hi all,
I'm just trying to write a recursive function that iterates=B9 on a str= ing
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 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<= br> =A0writing functions by using pattern matching
=B2 I read about micmatch but I'd like to avoid non standard packages.<= br> --
--
Caml-list mailing list. =A0Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners<= /a>
Bug reports:
http://caml.inria.fr/bin/caml-bugs


--bcaec5485c94835f4804aced02d0--