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 nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 695D2BBAF for ; Fri, 2 Jun 2006 17:45:58 +0200 (CEST) Received: from mail8.sea5.speakeasy.net (mail8.sea5.speakeasy.net [69.17.117.10]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k52Fjud3021276 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Fri, 2 Jun 2006 17:45:57 +0200 Received: (qmail 21682 invoked from network); 2 Jun 2006 15:45:55 -0000 Received: from uswa163.isomedia.com (HELO [192.168.1.2]) (shawnw@[207.115.79.163]) (envelope-sender ) by mail8.sea5.speakeasy.net (qmail-ldap-1.03) with SMTP for ; 2 Jun 2006 15:45:55 -0000 Message-ID: <44805D5C.2080604@speakeasy.org> Date: Fri, 02 Jun 2006 08:46:36 -0700 From: Shawn User-Agent: Thunderbird 1.5.0.2 (Macintosh/20060308) MIME-Version: 1.0 To: julien.michel@etu.univ-orleans.fr Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] adding lots of elements to a list References: <1149262126.4480592e24237@webmailetu.univ-orleans.fr> In-Reply-To: <1149262126.4480592e24237@webmailetu.univ-orleans.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 44805D34.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; shawnw:01 univ-orleans:01 iteration:01 decr:01 printf:01 printf:01 appending:01 garbage:01 recursive:01 rec:01 wrote:01 caml-list:01 michel:01 speakeasy:01 binding:02 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=SPF_SOFTFAIL autolearn=disabled version=3.0.3 julien.michel@etu.univ-orleans.fr wrote: > Can someone advise me a better way to do what I want ? > > Lists sound like the best choice for now. If memory becomes a problem, a disk-based db like gdbm might be an alternative. But until you /know/ that the lists are a bottleneck, don't bother. > > By the way, I have some difficulties to simply add a new element to the same > list, each time I enter in a while-loop: > > let count = ref 3 ;; (* number of iteration *) > let list = [] in > > while (!count > 0) do > decr count; > let list = list@[!count] in > Printf.printf "The 1st element is %i \n" (List.hd list) ; > done; > > Printf.printf "list contains %i elements \n" (List.length list) ;; > > > Here is the result after running the program: > The 1st element is 2 > The 1st element is 1 > The 1st element is 0 > list contains 0 elements > You're creating a new binding named list each time you do let list = ..., not updating an existing one. Outside of the while loop, the fir st, outer one is what's visible and used. You need references: ... let list = ref [] ... while !count > 0 do ... list := !count :: !list .... done Also, avoid appending to the list like you do if it's going to be big, as doing so will cause a lot of extra work for the garbage collector. Alternatively, do away with the while, and use a recursive function, and eliminate the need for references at all. Something like: let rec count_list count lst = let count = count - 1 in if count > 0 then count_list count (count :: lst) else lst let list = count_list 3 []