caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: julien.michel@etu.univ-orleans.fr
To: caml-list@yquem.inria.fr
Subject: adding lots of elements to a list
Date: Fri,  2 Jun 2006 17:28:46 +0200	[thread overview]
Message-ID: <1149262126.4480592e24237@webmailetu.univ-orleans.fr> (raw)

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




             reply	other threads:[~2006-06-02 15:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-02 15:28 julien.michel [this message]
2006-06-02 15:40 ` [Caml-list] " Jonathan Roewen
2006-06-02 15:46 ` Shawn
2006-06-02 15:55 ` Nils Gesbert
2006-06-02 22:06 ` Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1149262126.4480592e24237@webmailetu.univ-orleans.fr \
    --to=julien.michel@etu.univ-orleans.fr \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).