caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Nils Gesbert <nils.gesbert@ens.fr>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] adding lots of elements to a list
Date: Fri, 2 Jun 2006 17:55:12 +0200	[thread overview]
Message-ID: <20060602175512.17e5a8b5.nils.gesbert@ens.fr> (raw)
In-Reply-To: <1149262126.4480592e24237@webmailetu.univ-orleans.fr>

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.


  parent reply	other threads:[~2006-06-02 15:55 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
2006-06-02 15:55 ` Nils Gesbert [this message]
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=20060602175512.17e5a8b5.nils.gesbert@ens.fr \
    --to=nils.gesbert@ens.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).