* Unexpected behaviour of strings initialized with quotes
@ 2005-11-23 10:20 Arthur Chargueraud
2005-11-23 20:56 ` [Caml-list] " Martin Jambon
0 siblings, 1 reply; 2+ messages in thread
From: Arthur Chargueraud @ 2005-11-23 10:20 UTC (permalink / raw)
To: caml-list
I am surprized by a difference of behaviour between the strings
"bbbb" and (String.make 4 'b').
I have been programming in Caml for a while, and I've always
assumed that the two expression would be rather equivalent.
The idea is that when writing: let f() = "bbbb"
Any call to function f() will return the same string (ie same pointer).
This is of course not the case when writing: let f() = String.make 4 'b'
Below is a program where this behaviour is causing a problem:
code:
for i = 0 to 3 do
let s = "bbbb" in
s.[i] <- 'a';
Printf.printf "string s is now %s\n" s;
done;
outputs:
string s is now abbb
string s is now aabb
string s is now aaab
string s is now aaaa
What it means is that this code is just equivalent to:
let s = "bbbb" in
for i = 0 to 3 do
s.[i] <- 'a';
Printf.printf "string s is now %s\n" s;
done;
which I find really unexpected. I was waiting for:
string s is now abbb
string s is now babb
string s is now bbab
string s is now bbba
which is what happens when using (String.make 4 'b') inside the loop.
This is not a real problem, since it is not usual to modify
strings initialized with quotes, but I am just wandering
about the reason of such a behaviour...
Arthur Chargueraud
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Unexpected behaviour of strings initialized with quotes
2005-11-23 10:20 Unexpected behaviour of strings initialized with quotes Arthur Chargueraud
@ 2005-11-23 20:56 ` Martin Jambon
0 siblings, 0 replies; 2+ messages in thread
From: Martin Jambon @ 2005-11-23 20:56 UTC (permalink / raw)
To: Arthur Chargueraud; +Cc: caml-list
On Wed, 23 Nov 2005, Arthur Chargueraud wrote:
> I am surprized by a difference of behaviour between the strings
> "bbbb" and (String.make 4 'b').
...
> This is not a real problem, since it is not usual to modify
> strings initialized with quotes, but I am just wandering
> about the reason of such a behaviour...
It's actually easier to write String.copy "abc" instead of "abc"
than:
let f =
let constant = "abc"
fun x ->
...
do_something_with constant
...
instead of:
let f x =
...
do_something_with "abc"
...
which happens all the time.
--
Martin Jambon, PhD
http://martin.jambon.free.fr
Store and share your bioinformatics tips at http://wikiomics.org
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-11-23 20:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-23 10:20 Unexpected behaviour of strings initialized with quotes Arthur Chargueraud
2005-11-23 20:56 ` [Caml-list] " Martin Jambon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox