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 54FBFBB84 for ; Fri, 2 Jun 2006 17:28:39 +0200 (CEST) Received: from ka.univ-orleans.fr (ka.univ-orleans.fr [193.49.83.5]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k52FScg8019036 for ; Fri, 2 Jun 2006 17:28:38 +0200 Received: from localhost (localhost [127.0.0.1]) by ka.univ-orleans.fr (Postfix) with ESMTP id 893FE12AD62 for ; Fri, 2 Jun 2006 17:28:38 +0200 (CEST) Received: from 81.80.205.253 ([81.80.205.253]) by webmailetu.univ-orleans.fr (IMP) with HTTP for ; Fri, 2 Jun 2006 17:28:46 +0200 Message-ID: <1149262126.4480592e24237@webmailetu.univ-orleans.fr> Date: Fri, 2 Jun 2006 17:28:46 +0200 From: julien.michel@etu.univ-orleans.fr To: caml-list@yquem.inria.fr Subject: adding lots of elements to a list MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.2.1 X-Originating-IP: 81.80.205.253 X-Miltered: at nez-perce with ID 44805926.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; univ-orleans:01 arrays:01 iteration:01 decr:01 printf:01 printf:01 decr:01 trivial:01 filiere:01 specialite:98 exception:01 michel:01 michel:01 expression:01 instances: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.2 required=5.0 tests=NO_REAL_NAME autolearn=disabled version=3.0.3 An application creates as many instances of a class as they are iterations in a while-loop. I would like to store these objects once they are created, at each loop, in order to operates on them later (to sort, to filter some elements etc...) For example I would like to add each new instance to a list of object of this class. The number of created objects can grow very fast, and may raise an amount greater than 100 000 elements. At the beginning, I choose to store them in a list, because we can add/delete elements without wondering about size problem... Actually, in my application, I can not know how many elements I will add. So It is quite difficult to use arrays. On the other hand, I heard there were some problems with lists wich are very long... Can someone advise me a better way to do what I want ? 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 The first 3 lines show that new elements are well added to the "local" list, inside the loop but we can not access to the list outside the while loop, according to the last line. I try to declare "list" as a global variable: let count = ref 3 ;; let list = [] ;; while (!count > 0) do decr count; list = [!count]@list ; Printf.printf "The 1st element is %i \n" (List.hd list) ; done; Printf.printf "list contains %i elements \n" (List.length list) ;; But this time I get a "Warning S: this expression (list = [!count]@list ;) should have type unit." and I get the following error while running the program: Fatal error: exception Failure("hd") It seems that no elements are added to the list, which remains empty. I hope this trivial question won't bother too much people... Thank you all -- MICHEL Julien Elève Ingénieur 5ème année Polytech'Orléans filière Electronique-Signaux-Images (ESI) spécialité Systèmes Embarqués (SE) 200 Rue Ardoux Bat A, appt 43 45160 OLIVET