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 4DBD0BB81 for ; Fri, 22 Jul 2005 20:59:52 +0200 (CEST) Received: from rome.ucdavis.edu (rome.ucdavis.edu [169.237.104.165]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j6MIxocO021498 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Fri, 22 Jul 2005 20:59:51 +0200 Received: from [169.237.7.95] (puck.cs.ucdavis.edu [169.237.7.95]) by rome.ucdavis.edu (8.13.3/8.13.1/it-defang-5.4.0) with ESMTP id j6MIxj5U016304; Fri, 22 Jul 2005 11:59:46 -0700 (PDT) Message-ID: <42E141E3.6080107@crans.org> Date: Fri, 22 Jul 2005 11:58:43 -0700 From: Stephane Glondu User-Agent: Debian Thunderbird 1.0.2 (X11/20050331) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Christophe Dehlinger Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] How to do this properly with OCaml? References: <42E1103C.4020602@ifp.fr> In-Reply-To: <42E1103C.4020602@ifp.fr> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.49 on 169.237.104.165 X-Miltered: at concorde with ID 42E14226.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 christophe:01 arrays:01 rec:01 foo:01 foo:01 right-hand:01 rec':01 recursive:01 intuitively:01 compiler:01 rec:01 compiler:01 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 Hi, Christophe Dehlinger wrote: > I have my own array-related problem, so I'll plug it here :) > A couple of times, I would have liked to build arrays of functions whose > body uses the array itself, like: > > #let rec foo = Array.init 5 (fun n () -> if n=0 then 0 else foo.(n/2) ()) > This kind of expression is not allowed as right-hand side of `let rec' > > Why is this kind of construct forbidden ? In general, you cannot define a recursive value as a result of a computation which uses the being-defined value. Intuitively, I explain it in that way: here, the second argument references sth which is created by Array.init itself, so the compiler has to know the result of the call before compiling the argument function, which is clearly impossible. You have to do sth like this: let foo = let bar = Array.make 5 (fun () -> 0) in for n = 1 to 4 do bar.(n) <- (fun () -> bar.(n/2) ()) done; bar;; However, you can do this: let rec foo = [| (fun () -> 0); (fun () -> foo.(0) ()) |];; (here, the array is created by the compiler itself, so it knows its location). I hope this is clear. Stephane Glondu.