From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA27679 for caml-redist@pauillac.inria.fr; Sun, 14 May 2000 23:12:55 +0200 (MET DST) Resent-Message-Id: <200005142112.XAA27679@pauillac.inria.fr> 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 WAA13161 for ; Fri, 12 May 2000 22:00:04 +0200 (MET DST) Received: from babbage.ececs.uc.edu (mail.ececs.uc.edu [129.137.8.2]) by concorde.inria.fr (8.10.0/8.10.0) with ESMTP id e4CJxr905809; Fri, 12 May 2000 21:59:58 +0200 (MET DST) Received: from gatekeeper-internal.ececs.uc.edu (gatekeeper-internal.ececs.uc.edu [129.137.8.10]) by babbage.ececs.uc.edu (8.9.3+Sun/8.9.3) with ESMTP id PAA24493; Fri, 12 May 2000 15:59:39 -0400 (EDT) Date: Fri, 12 May 2000 15:59:39 -0400 (EDT) From: Hongwei Xi To: Pierre Weis cc: caml-list@inria.fr Subject: Re: reference initialization In-Reply-To: <200005121707.TAA26883@pauillac.inria.fr> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Resent-From: weis@pauillac.inria.fr Resent-Date: Sun, 14 May 2000 23:12:55 +0200 Resent-To: caml-redist@pauillac.inria.fr > Not exactly, you just have to find one element into a. For instance: > let alen = Array.length a in > if alen = 0 then b (or Array.copy b if you need a fresh array) else > let ? = a.(0) in > let c = ... > > for i = 1 to alen - 1 do > ... But, why? Why should we first initialize c with such a value. I don't think you have made a program safer by doing so. > # Array.init;; > - : int -> f:(int -> 'a) -> 'a array = > > Using it, you don't need to bother with any dummy initialization value: > > let combine_arrays a b = > let alen = Array.length a in > let blen = Array.length b in > let init i = if i < alen then a.(i) else b.(i-alen) in > Array.init (alen + blen) init This is great in this case. But suppose that I want to compute the combinatorial numbers 'n chooses k' for k= 0,..,n. let chooses (n) = let result = Array.array (n+1) ? in begin result.(0) <- 1; result.(n) <- 1; for i = 1 to n/2 do result.(i) <- result.(i-1) * (n-i+1) / i; result.(n-i) <- result.(i); done; result end (* code is not tested *) Certainly, we can replace ? with 0. But what is really achieved? I would say it is simply an illusion that a program is made safer by initializing each array upon its allocation. Actually, a program is likely made faster but unsafer by doing so (since no nullity checking is needed but a wrong value may be supplied to continue a computation that should be aborted) Best, --Hongwei \~~~~/ \\ // \\ // @ Mail: hwxi@ececs.uc.edu C-o^o, ))__|| \\__//_ // \\ Url: http://www.ececs.uc.edu/~hwxi ( ^ ) ))__|| \--/-\\ \\ Tel: +1 513 871 4947 (home) / \V\ )) || // \\ \\ Tel: +1 513 556 4762 (office) ------ // || o // \\ \\//Fax: +1 513 556 7326 (department) Rhodes Hall 811-D Department of ECE & CS University of Cincinnati P. O. Box 210030 Cincinnati, OH 45221-0030 On Fri, 12 May 2000, Pierre Weis wrote: > > > (2) If you really want to make sure that 'c' is well-initialized, > > you should probably check this after those two loops. The question > > is how to incorporate the checking result into the type system. > > (3) If you initialize 'c' with a (wrong) value, it seems to me > > that nothing is achieved. > > (4) Also, the problem cannot be solved using option type. > > > > This is a precise senario that I had in mind, where the kind of > > mandatory array initialization in ML-like langugages is simply > > inappropriate, isn't it? > > You should consider that there is a general initialisation function in > the Array module, named Array.init, that allocates for you a fresh > array then fill it with values obtained by calling an arbitrary > supplied function: > > > This code ensures the ``always initialized strategy'' of ML, and seems > to me elegant and clear (but note that it uses higher-order > functionality). Have I missed something ? > > Best regards, > > PS: An even shorter version of combine_arrays should be > let combine_arrays = Array.append > > Pierre Weis > > INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/ > >