caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Shawn <shawnw@speakeasy.org>
To: julien.michel@etu.univ-orleans.fr
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] adding lots of elements to a list
Date: Fri, 02 Jun 2006 08:46:36 -0700	[thread overview]
Message-ID: <44805D5C.2080604@speakeasy.org> (raw)
In-Reply-To: <1149262126.4480592e24237@webmailetu.univ-orleans.fr>

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 []


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

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-02 15:28 julien.michel
2006-06-02 15:40 ` [Caml-list] " Jonathan Roewen
2006-06-02 15:46 ` Shawn [this message]
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=44805D5C.2080604@speakeasy.org \
    --to=shawnw@speakeasy.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=julien.michel@etu.univ-orleans.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).