* confusion about mutable strings
@ 2008-02-10 17:46 Ralph Douglass
2008-02-10 18:03 ` [Caml-list] " Bünzli Daniel
0 siblings, 1 reply; 6+ messages in thread
From: Ralph Douglass @ 2008-02-10 17:46 UTC (permalink / raw)
To: Caml List
[-- Attachment #1: Type: text/plain, Size: 608 bytes --]
I'm a little confused by some behavior I'm seeing. Here is an example from
the toplevel:
# let foo () =
let str = "ffff" in
Printf.printf "%s\n%!" str;
str.[0] <- 'r';
str.[1] <- 'r';
str.[2] <- 'r';
str.[3] <- 'r';
str
;;
val foo : unit -> string = <fun>
# foo ();;
ffff
- : string = "rrrr"
# foo ();;
rrrr
- : string = "rrrr"
#
Why isn't the output for the second printf also "ffff"? It seems like it's
completely ignoring "let str =" the second time.
Any thoughts? Is this a bug, or something I just don't know about?
3.10.1 on intel osx 10.5.1, installed from godi.
Thanks,
Ralph
[-- Attachment #2: Type: text/html, Size: 893 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] confusion about mutable strings
2008-02-10 17:46 confusion about mutable strings Ralph Douglass
@ 2008-02-10 18:03 ` Bünzli Daniel
2008-02-11 0:22 ` Ralph Douglass
0 siblings, 1 reply; 6+ messages in thread
From: Bünzli Daniel @ 2008-02-10 18:03 UTC (permalink / raw)
To: Ralph Douglass; +Cc: Caml List
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] confusion about mutable strings
2008-02-10 18:03 ` [Caml-list] " Bünzli Daniel
@ 2008-02-11 0:22 ` Ralph Douglass
2008-02-11 10:01 ` Loup Vaillant
0 siblings, 1 reply; 6+ messages in thread
From: Ralph Douglass @ 2008-02-11 0:22 UTC (permalink / raw)
To: Bünzli Daniel; +Cc: Caml List
[-- 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 --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] confusion about mutable strings
2008-02-11 0:22 ` Ralph Douglass
@ 2008-02-11 10:01 ` Loup Vaillant
2008-02-11 13:46 ` Ralph Douglass
0 siblings, 1 reply; 6+ messages in thread
From: Loup Vaillant @ 2008-02-11 10:01 UTC (permalink / raw)
To: Ralph Douglass; +Cc: Bünzli Daniel, Caml List
2008/2/11, Ralph Douglass <ralph@grayskies.net>:
>
> 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?
C strings are usually "magically special" in exactly the same way. It
has been talked about in another thread (dunno how to look for it).
Basically, this is a design choice meant for better performance.
Loup
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] confusion about mutable strings
2008-02-11 10:01 ` Loup Vaillant
@ 2008-02-11 13:46 ` Ralph Douglass
2008-02-12 19:33 ` Ashish Agarwal
0 siblings, 1 reply; 6+ messages in thread
From: Ralph Douglass @ 2008-02-11 13:46 UTC (permalink / raw)
To: Caml List
[-- Attachment #1: Type: text/plain, Size: 1030 bytes --]
Sorry, I was indeed informed today by someone at work that it was talked
about a week or two ago. There is so much going on on the list that I just
missed it. And now I added noise myself. Sigh.
Thanks,
Ralph
On 2/11/08, Loup Vaillant <loup.vaillant@gmail.com> wrote:
>
> 2008/2/11, Ralph Douglass <ralph@grayskies.net>:
> >
> > 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?
>
> C strings are usually "magically special" in exactly the same way. It
> has been talked about in another thread (dunno how to look for it).
> Basically, this is a design choice meant for better performance.
>
> Loup
>
--
Ralph
[-- Attachment #2: Type: text/html, Size: 1649 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] confusion about mutable strings
2008-02-11 13:46 ` Ralph Douglass
@ 2008-02-12 19:33 ` Ashish Agarwal
0 siblings, 0 replies; 6+ messages in thread
From: Ashish Agarwal @ 2008-02-12 19:33 UTC (permalink / raw)
To: Caml List, Ralph Douglass
[-- Attachment #1: Type: text/plain, Size: 405 bytes --]
Here's a link to the previous thread:
http://caml.inria.fr/pub/ml-archives/caml-list/2008/01/4ca0f4bdf642d5fdcda87a82c3f70a85.en.html
2008/2/11 Ralph Douglass <ralph@grayskies.net>:
> Sorry, I was indeed informed today by someone at work that it was talked
about a week or two ago. There is so much going on on the list that I just
missed it. And now I added noise myself. Sigh.
>
> Thanks,
>
> Ralph
>
[-- Attachment #2: Type: text/html, Size: 630 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-02-12 19:33 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-10 17:46 confusion about mutable strings Ralph Douglass
2008-02-10 18:03 ` [Caml-list] " Bünzli Daniel
2008-02-11 0:22 ` Ralph Douglass
2008-02-11 10:01 ` Loup Vaillant
2008-02-11 13:46 ` Ralph Douglass
2008-02-12 19:33 ` Ashish Agarwal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox