caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Arthur Chargueraud" <cours_caml@france-ioi.org>
To: <caml-list@yquem.inria.fr>
Subject: Unexpected behaviour of strings initialized with quotes
Date: Wed, 23 Nov 2005 11:20:09 +0100	[thread overview]
Message-ID: <002601c5f017$7ba6cc10$e600a8c0@arthur> (raw)

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


             reply	other threads:[~2005-11-23 10:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-23 10:20 Arthur Chargueraud [this message]
2005-11-23 20:56 ` [Caml-list] " Martin Jambon

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='002601c5f017$7ba6cc10$e600a8c0@arthur' \
    --to=cours_caml@france-ioi.org \
    --cc=caml-list@yquem.inria.fr \
    /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).