caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jonathan Roewen" <jonathan.roewen@gmail.com>
To: "julien.michel@etu.univ-orleans.fr" <julien.michel@etu.univ-orleans.fr>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] adding lots of elements to a list
Date: Sat, 3 Jun 2006 03:40:28 +1200	[thread overview]
Message-ID: <ad8cfe7e0606020840i23caee2fld3f8d220690f1e41@mail.gmail.com> (raw)
In-Reply-To: <1149262126.4480592e24237@webmailetu.univ-orleans.fr>

> 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) ;;

The let in the function is local, so is completely different to the
global list value. You'd want to use a ref.

let list = ref [];;

while ... do
  list := !list @ [!count];
done

(* btw, the output proves you're not adding to the global list, as the
head of the list should never change, as you're concatenating to the
end of the list *)

(* as a further aside, concatenating two lists takes time proportional
to the length of the first argument (your global list). prepending is
faster, though ends up with a reversed list, as in: list := !count ::
!list *)

> 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;

*** 'list = [!count] @ list' is a boolean equation. It in fact tests
if list is structurally equivalent to [!count] @ list, and generates
the value false, which is then discarded. Since list is never
modified, it is always empty, thus List.hd will raise the exception
you have witnessed.

> But this time I get a "Warning S: this expression (list = [!count]@list ;)
> should have type unit."

Yes, this is what I meant by the boolean equation -- it's return type
is bool, not unit.

Jonathan


  reply	other threads:[~2006-06-02 15:40 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 ` Jonathan Roewen [this message]
2006-06-02 15:46 ` [Caml-list] " 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=ad8cfe7e0606020840i23caee2fld3f8d220690f1e41@mail.gmail.com \
    --to=jonathan.roewen@gmail.com \
    --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).