From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=HTML_MESSAGE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 36251BC6B for ; Mon, 11 Feb 2008 01:22:23 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAADMkr0fAXQImh2dsb2JhbACCPDWNSgEBAQgKKZVd X-IronPort-AV: E=Sophos;i="4.25,331,1199660400"; d="scan'208";a="7145721" Received: from discorde.inria.fr ([192.93.2.38]) by mail2-smtp-roc.national.inria.fr with ESMTP; 11 Feb 2008 01:22:22 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id m1B0MMcX023674 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Mon, 11 Feb 2008 01:22:22 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAAkkr0dA6aa2i2dsb2JhbACCPDWNSgEBAQgEBAkKEQWVXg X-IronPort-AV: E=Sophos;i="4.25,331,1199660400"; d="scan'208";a="7880184" Received: from py-out-1112.google.com ([64.233.166.182]) by mail1-smtp-roc.national.inria.fr with ESMTP; 11 Feb 2008 01:22:22 +0100 Received: by py-out-1112.google.com with SMTP id u52so6545096pyb.10 for ; Sun, 10 Feb 2008 16:22:20 -0800 (PST) Received: by 10.142.131.18 with SMTP id e18mr8104639wfd.207.1202689340248; Sun, 10 Feb 2008 16:22:20 -0800 (PST) Received: by 10.142.179.16 with HTTP; Sun, 10 Feb 2008 16:22:20 -0800 (PST) Message-ID: <71767b800802101622j45fd0db4id4351b44fb1f9cf3@mail.gmail.com> Date: Sun, 10 Feb 2008 19:22:20 -0500 From: "Ralph Douglass" To: "=?ISO-8859-1?Q?B=FCnzli_Daniel?=" Subject: Re: [Caml-list] confusion about mutable strings Cc: "Caml List" In-Reply-To: <8BDAF65E-F29B-4F8C-B20E-1E0FDA521609@erratique.ch> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_10740_19155031.1202689340240" References: <71767b800802100946o4d465b79xad9b1f9515f7e69f@mail.gmail.com> <8BDAF65E-F29B-4F8C-B20E-1E0FDA521609@erratique.ch> X-Miltered: at discorde with ID 47AF953E.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; mutable:01 ocaml:01 foo:01 iter:01 printf:01 printf:01 val:01 foo:01 ocaml:01 bunzli:01 buenzli:01 invocation:01 iter:01 val:01 bunzli:01 ------=_Part_10740_19155031.1202689340240 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sorry, I should have made clear that this is not a problem I wanted solved for me, but rather a question about OCaml. I've just never come across thi= s before because I don't usually mutate strings. Observe the following: # let foo () =3D let bar =3D [|'a';'b';'c'|] in Array.iter (Printf.printf "%c") bar; bar.(0) <- 'd'; bar;; val foo : unit -> char array =3D # foo ();; abc- : char array =3D [|'d'; 'b'; 'c'|] # foo ();; abc- : char array =3D [|'d'; 'b'; 'c'|] Why does OCaml treat these two examples in such a different manner? Is there a reason why strings are magically special in this way? On 2/10/08, B=FCnzli Daniel wrote: > > Each invocation of foo does not allocate a new string for str, "ffff" > is a constant string allocated once and you are updating this constant. > > let str =3D String.copy "ffff" > > will solve your problem. > > Best, > > Daniel > > --=20 Ralph ------=_Part_10740_19155031.1202689340240 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sorry, I should have made clear that this is not a problem I wanted solved = for me, but rather a question about OCaml.  I've just never come a= cross this before because I don't usually mutate strings.

Observ= e the following:

# let foo () =3D
  let bar =3D [|'a';'b';'c= '|] in
  Array.iter (Printf.printf "%c") bar;
&nbs= p; bar.(0) <- 'd';
  bar;;
val foo : unit -> char = array =3D <fun>
# foo ();;
abc- : char array =3D [|'d'; 'b'; 'c'= |]
# foo ();;
abc- : char array =3D [|'d'; 'b'; '= c'|]

Why does OCaml treat these two examples in such a different= manner?  Is there a reason why strings are magically special in this = way?

On 2/10/08, B=FCnzli Daniel <= daniel.buenzli@erratique.ch> wrote:
Each invocation of foo does not allocate a new string for str, "ffff&q= uot;
is a constant  string allocated once and you are updating= this constant.

let str =3D String.copy "ffff"

will= solve your problem.

Best,

Daniel



--
Ralph ------=_Part_10740_19155031.1202689340240--