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 546A5D176 for ; Tue, 26 Jul 2005 02:46:47 +0200 (CEST) Received: from cenn.mc.mpls.visi.com (cenn.mc.mpls.visi.com [208.42.156.9]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j6Q0kk09004785 for ; Tue, 26 Jul 2005 02:46:47 +0200 Received: from [192.168.42.2] (bhurt.dsl.visi.com [208.42.141.66]) by cenn.mc.mpls.visi.com (Postfix) with ESMTP id AC25C8107; Mon, 25 Jul 2005 19:46:45 -0500 (CDT) Date: Mon, 25 Jul 2005 19:47:16 -0500 (CDT) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: skaller Cc: Stephane Glondu , caml-list@yquem.inria.fr Subject: Re: [Caml-list] How to do this properly with OCaml? In-Reply-To: <1122291335.6766.33.camel@localhost.localdomain> Message-ID: References: <200507241623.13705.Stephane.Glondu@crans.org> <1122251570.9027.362.camel@localhost.localdomain> <200507242345.13152.Stephane.Glondu@crans.org> <1122291335.6766.33.camel@localhost.localdomain> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at concorde with ID 42E587F6.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 arrays:01 ocaml:01 mutable:01 mutable:01 blit:01 deque:01 char:01 char:01 2005,:98 ...:98 idx:98 idx:98 wrote: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.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On Mon, 25 Jul 2005, skaller wrote: > On Sun, 2005-07-24 at 23:45 -0700, Stephane Glondu wrote: > >> It would surely be interesting. But now, we have moved from "using >> Obj.magic for better efficiency" to "modifying to collector"... > > Well, basically the real topic is how to implement variable > length arrays. This is easy enough in C and C++: why should > it be very hard or even impossible in Ocaml? It is, in fact, neither- *IF* you don't mind a little bit of inefficiency. For example: type 'a t = { mutable len: int; mutable data: 'a option array; };; let make init_size = let init_size = if init_size <= 0 then 16 else init_size in { len = 0; data = Array.make init_size None } ;; let get arr idx = if (idx < 0) || (idx > arr.len) then invalid_arg "get_arr" else arr.data.(idx) ;; let append arr x = (* add x to the end of arr *) if (arr.len == (Array.length arr.data)) then begin let newarr = Array.make (2*arr.len) None in Array.blit arr.data 0 newarr 0 arr.len; arr.data <- newarr; end; arr.data.(arr.len) <- Some(x); arr.len <- arr.len + 1; () ;; It's insisting that it be done without options that's tricky. As a side note, whenever I or anyone else starts bitching about how something is easy to do in C but hard to do in Ocaml, that's a sign that I'm approaching the problem wrong. Most likely, you need some sort of deque or other data structure. > The language requirements with respect to initialisation > are the difference: Ocaml requires all store to be > initialised, C/C++ does not. Yep. The following C code is really hard to implement in Ocaml: char * ptr = (char *) 0xA00000ul; ptr[315] = 'a'; I consider this an advantage of Ocaml over C/C++. Brian