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 nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id C7E09BB83 for ; Thu, 25 May 2006 13:13:45 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k4PBDifZ032258 for ; Thu, 25 May 2006 13:13:44 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA08395 for ; Thu, 25 May 2006 13:13:44 +0200 (MET DST) Received: from luna.vie.lunde.net (at.lunde.net [62.116.13.60]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k4PBDh0u023047 for ; Thu, 25 May 2006 13:13:44 +0200 Received: from h-68-166-100-33.nycmny83.covad.net ([68.166.100.33] helo=localhost.localdomain) by luna.vie.lunde.net with asmtp (Exim 3.36 #1 (Debian)) id 1FjDmf-0001ip-00; Thu, 25 May 2006 13:13:41 +0200 Received: from localhost (localhost.localdomain [127.0.0.1]) by localhost.localdomain (Postfix) with ESMTP id C66787924D; Thu, 25 May 2006 07:14:55 -0400 (EDT) Date: Thu, 25 May 2006 07:14:55 -0400 (EDT) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: Martin Jambon Cc: Stefan Monnier , caml-list@inria.fr Subject: Re: [Caml-list] Re: immutable strings (Re: Array 4 MB size limit) In-Reply-To: Message-ID: References: <20060515141230.ajyupn2z28k0484s@horde.akalin.cx> <446D5E4A.8060005@akalin.cx> <20060519162844.GA32550@osiris.uid0.sk> <200605192226.34917.jon@ffconsultancy.com> <20060520211117.GA2670@first.in-berlin.de> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at nez-perce with ID 44759168.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 44759167.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; chunks:01 semantics:01 applicative:01 ocaml:01 ocaml's:01 foo:01 foo:01 ocaml:01 compares:01 printf:01 mutable:01 eliminates:98 wrote:01 caml-list:01 char:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.3 On Wed, 24 May 2006, Martin Jambon wrote: > And obviously it's not possible to handle only immutable strings since > somehow you have to create them, and unlike record fields, they won't be > set in one operation but in n operations, n being the length of the > string. This is the advantage of regular expressions- they can be implemented on immutable strings (regular expressions don't modify the old strings, they produce new strings), and can change large chunks of the string in one "operation", limiting the number of unnecessary string copies you need to do. Yes, the underlying implementation would be mutable- but so is they underlying implementation of everything else. The semantics remain applicative. Note that this also eliminates an inefficiency of ocaml. When you use a constant string, it has to allocate a new string for you. Consider Ocaml's response to the expression: "foo" == "foo";; The reason ocaml returns false in the above expression is that when you give a constant string in an expression, Ocaml first has to make a copy of that string (in case you modify it), and then it returns the copy. So it makes two copies of the string "foo" above, and then compares them for referential equality- which, since they're different copies, returns false. Note that even pure-imperitive languages, like C/C++, hit this problem. What is the output of the following peice of C code: for (i = 0; i < 10; ++i) { char * p = "foo"; p[0] += 1; printf("%s\n", p); } C/C++ itself wants *some* strings to be immutable. Fortran used to let you change the value of 2, which made for some interesting (in the bad sense) code tricks. I comment that they've since fixed this. But mutable strings allow you to change the value of "two", which is almost as bad. Brian