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 5EC78BB81 for ; Mon, 26 Sep 2005 02:24:03 +0200 (CEST) Received: from rwcrmhc12.comcast.net (rwcrmhc12.comcast.net [216.148.227.85]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j8Q0O2jB011825 for ; Mon, 26 Sep 2005 02:24:02 +0200 Received: from 192.168.1.100 (c-24-118-210-113.hsd1.mn.comcast.net[24.118.210.113]) by comcast.net (rwcrmhc12) with SMTP id <20050926002400014000r140e>; Mon, 26 Sep 2005 00:24:00 +0000 Subject: Re: [Caml-list] Avoiding shared data From: Bill Wood To: Martin Chabr Cc: caml-list@yquem.inria.fr In-Reply-To: <20050925213202.32862.qmail@web26805.mail.ukl.yahoo.com> References: <20050925213202.32862.qmail@web26805.mail.ukl.yahoo.com> Content-Type: text/plain Date: Sun, 25 Sep 2005 19:23:59 -0500 Message-Id: <1127694239.1060.23.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 2.0.1-1mdk Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 43373FA2.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 avoiding:01 arrays:01 pairs:01 integers:01 arrays:01 val:01 comcast:98 ...:98 ...:98 constants:01 structures:01 structures:01 functions:01 int:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.9 required=5.0 tests=DNS_FROM_RFC_POST, DNS_FROM_RFC_WHOIS,FORGED_RCVD_HELO autolearn=disabled version=3.0.3 . . . > I need to process arrays of pairs of integers and > records ((int * record) array) in which all elements > must be updated individually, which means that > unshared data structures must be used. For arrays of > arrays I can produce unshared data by using the > library functions Array.copy and Array.append to > append the individual arrays into the embedding array. > It works, the low level arrays can be updated > individually. But I cannot use the same scheme to the > array of the (int * record) structures, because I do > not know how to copy these structures to dissolve the > sharing. I do not even know how to copy records. It > seems to me that this problem occurs always when I > want to produce an array of data with a fixed > structure automatically (rather than entering the > array [| ... |] by hand at the top level interpreter > using constants only). How can I produce completely > unshared structures? Funny you should mention it, I wrestled with this just this week. I'm building a square, i.e. array of N elements, each of which is itself an array of N cells, where a cell is a record with several fields. Here's the way I did it: 1) I defined a function initial_cell : unit -> cell as let initial_cell () = let c = {field1 = val1; ...; fieldN = valN} in c;; 2) I defined a function initial_row : int -> cell array as: let initial_row n = Array.init n (fun i -> initial_cell ());; 3) I defined a function initial_square : int -> cell array array by: let initial_square n = Array.init n (fun i -> initial_row ());; I think the essential point is the Array.init function; since it expects to set the i-th element of the new array to (f i) for unknown f, it can't make an array that shares. I hope this is of some help, -- Bill Wood bill.wood@acm.org