caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Ralph Douglass" <ralph@grayskies.net>
To: "Bünzli Daniel" <daniel.buenzli@erratique.ch>
Cc: "Caml List" <caml-list@inria.fr>
Subject: Re: [Caml-list] confusion about mutable strings
Date: Sun, 10 Feb 2008 19:22:20 -0500	[thread overview]
Message-ID: <71767b800802101622j45fd0db4id4351b44fb1f9cf3@mail.gmail.com> (raw)
In-Reply-To: <8BDAF65E-F29B-4F8C-B20E-1E0FDA521609@erratique.ch>

[-- Attachment #1: Type: text/plain, Size: 955 bytes --]

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 this
before because I don't usually mutate strings.

Observe the following:

# let foo () =
  let bar = [|'a';'b';'c'|] in
  Array.iter (Printf.printf "%c") bar;
  bar.(0) <- 'd';
  bar;;
val foo : unit -> char array = <fun>
# foo ();;
abc- : char array = [|'d'; 'b'; 'c'|]
# foo ();;
abc- : char array = [|'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ünzli Daniel <daniel.buenzli@erratique.ch> 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 = String.copy "ffff"
>
> will solve your problem.
>
> Best,
>
> Daniel
>
>


-- 
Ralph

[-- Attachment #2: Type: text/html, Size: 1458 bytes --]

  reply	other threads:[~2008-02-11  0:22 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-10 17:46 Ralph Douglass
2008-02-10 18:03 ` [Caml-list] " Bünzli Daniel
2008-02-11  0:22   ` Ralph Douglass [this message]
2008-02-11 10:01     ` Loup Vaillant
2008-02-11 13:46       ` Ralph Douglass
2008-02-12 19:33         ` Ashish Agarwal

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=71767b800802101622j45fd0db4id4351b44fb1f9cf3@mail.gmail.com \
    --to=ralph@grayskies.net \
    --cc=caml-list@inria.fr \
    --cc=daniel.buenzli@erratique.ch \
    /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).