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 5D152BB84 for ; Fri, 2 Jun 2006 17:55:30 +0200 (CEST) Received: from nef2.ens.fr (nef2.ens.fr [129.199.96.40]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k52FtTZN001054 for ; Fri, 2 Jun 2006 17:55:30 +0200 Received: from di.ens.fr (di.ens.fr [129.199.99.1]) by nef2.ens.fr (8.13.6/1.01.28121999) with ESMTP id k52FtT6h045171 for ; Fri, 2 Jun 2006 17:55:29 +0200 (CEST) X-Envelope-To: Received: from cduce (dhcp151.dmi.ens.fr [129.199.97.151]) by di.ens.fr (8.12.9/jb-1.1) id k52FtQVF020292 for ; Fri, 2 Jun 2006 17:55:26 +0200 (MEST) Date: Fri, 2 Jun 2006 17:55:12 +0200 From: Nils Gesbert To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] adding lots of elements to a list Message-Id: <20060602175512.17e5a8b5.nils.gesbert@ens.fr> In-Reply-To: <1149262126.4480592e24237@webmailetu.univ-orleans.fr> References: <1149262126.4480592e24237@webmailetu.univ-orleans.fr> X-Mailer: Sylpheed version 2.2.1 (GTK+ 2.8.13; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-milter (http://amavis.org/) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-1.5.10 (nef2.ens.fr [129.199.96.32]); Fri, 02 Jun 2006 17:55:29 +0200 (CEST) X-Miltered: at concorde with ID 44805F72.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; nils:01 nils:01 univ-orleans:01 printf:01 printf:01 iteration:01 recursive:01 rec:01 2006:98 wrote:01 caml-list:01 constructor:01 michel:01 define:01 expression: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 Fri, 2 Jun 2006 17:28:46 +0200 julien.michel@etu.univ-orleans.fr wrote: > let list = list@[!count] in > Printf.printf "The 1st element is %i \n" (List.hd list) ; What you are doing with this expression is not to modify the list "list" (which you cannot do, because lists are immutable) but to define a new variable, which happens to have the same name "list", whose scope is just the printf expression. So it is not even true that new elements are well added to the local list. You may see better the meaning of this code by using a different identifier for the local variable : your code is equivalent to let new_list = list@[!count] in Printf.printf "The 1st element is %i \n" (List.hd new_list) so the original list is never modified and each iteration creates a new list. To construct a list step by step, you may either use a reference and a loop or use a recursive function, like this for instance : let list = let rec add_elem list count = if count > 0 then add_elem (count::list) (count-1) else list in add_elem [] 3 By the way, you should construct the list by always adding elements at the head (with the :: constructor) and avoid @, because @ is a function that has to go through the whole list to reach the end, take your new element then reconstruct the whole list (the lists are constructed backwards), whereas adding an element at start is immediate.