From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 2D02BBB9C for ; Wed, 23 Nov 2005 11:20:12 +0100 (CET) Received: from mail.aquaray.com (feyd.aquaray.com [193.19.216.178]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jANAKBDe018568 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 23 Nov 2005 11:20:11 +0100 Received: (qmail 12004 invoked by uid 89); 23 Nov 2005 11:18:57 +0100 Received: from 128-240-118-80.kaptech.net (HELO arthur) (cours?caml@france-ioi.org@80.118.240.128) by mail.aquaray.com with SMTP; 23 Nov 2005 11:18:57 +0100 Message-ID: <002601c5f017$7ba6cc10$e600a8c0@arthur> Reply-To: "Arthur Chargueraud" From: "Arthur Chargueraud" To: Subject: Unexpected behaviour of strings initialized with quotes Date: Wed, 23 Nov 2005 11:20:09 +0100 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Miltered: at concorde with ID 4384425B.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; initialized:01 pointer:01 printf:01 printf:01 initialized:01 behaviour:01 behaviour:01 expression:01 strings:01 strings:01 caml:02 caml:02 string:02 string:02 programming:03 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 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