From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p3UDYZBJ029760 for ; Sat, 30 Apr 2011 15:34:36 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AooAAIgOvE1RZ90vkWdsb2JhbACYEo4FFAEBAQEJCwsHFCWIcblbhgAEjnmOIg X-IronPort-AV: E=Sophos;i="4.64,292,1301868000"; d="scan'208";a="94130031" Received: from mtaout01-winn.ispmail.ntl.com ([81.103.221.47]) by mail4-smtp-sop.national.inria.fr with ESMTP; 30 Apr 2011 15:34:30 +0200 Received: from aamtaout04-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout01-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20110430133429.NGSL14839.mtaout01-winn.ispmail.ntl.com@aamtaout04-winn.ispmail.ntl.com> for ; Sat, 30 Apr 2011 14:34:29 +0100 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout04-winn.ispmail.ntl.com (InterMail vG.3.00.04.00 201-2196-133-20080908) with ESMTP id <20110430133429.RWUX25656.aamtaout04-winn.ispmail.ntl.com@romulus.metastack.com> for ; Sat, 30 Apr 2011 14:34:29 +0100 Received: from remus.metastack.local ([172.16.0.1]) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id p3UDYPAY002717 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL) for ; Sat, 30 Apr 2011 14:34:26 +0100 Received: from Remus.metastack.local ([fe80::547c:3c42:e1da:eda2]) by Remus.metastack.local ([fe80::547c:3c42:e1da:eda2%11]) with mapi; Sat, 30 Apr 2011 14:34:25 +0100 From: David Allsopp To: "caml-list@inria.fr" Thread-Topic: [Caml-list] Differences between Array and Strings Thread-Index: AQHMByZFMy4Cde7a8ESwOoNziK5/G5R2YdNA Date: Sat, 30 Apr 2011 13:34:23 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.1 cv=R50lirqlHffDPPkwUlkuVa99MrvKdVWo//yz83qex8g= c=1 sm=0 a=gmb3etpw-7oA:10 a=cTs9vV391PwA:10 a=kj9zAlcOel0A:10 a=xqWC_Br6kY4A:10 a=XCYM_PRZtbwJTqn17mUA:9 a=CjuIK1q_8ugA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p3UDYZBJ029760 Subject: RE: [Caml-list] Differences between Array and Strings Radu Grigore wrote: > On Friday, April 29, 2011 12:51:59 PM UTC+1, louis....@ens.fr wrote: > > > let l () = "1" ;; > > > [...] > > > l()==l();; > > # val l : unit -> string = > > # [...] > > # - : bool = true > > Is there a good reason for this behavior? Whether it's good is debatable, but in most instances you don't want a fresh string being allocated each time for a constant value as it would be a waste of time and memory (most strings are used immutably). > My first thought was that that code should behave the same as > > let f () = ref () in f () == f () > (A satisfying explanation would *not* involve the compiler.) Look at the OCaml language specification - "1" is a constant and the result of that expression is the constant itself (6.7.1). [|1|] is an array expression and the result of that expression is a "1-element array, whose element(s) are initialized with the value(s) 1" (roughly quoting section 6.7.3). i.e. an array expression implies the creation of the array as its result but a constant evaluation does not. C behaves in the same (or at least in a very similar) way... not that that could ever be used as a justification for behaviour in a sane language like OCaml ;o) David