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 2C611BB84 for ; Fri, 2 Jun 2006 17:40:30 +0200 (CEST) Received: from nz-out-0102.google.com (nz-out-0102.google.com [64.233.162.205]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k52FeTV5020534 for ; Fri, 2 Jun 2006 17:40:29 +0200 Received: by nz-out-0102.google.com with SMTP id 13so536754nzn for ; Fri, 02 Jun 2006 08:40:29 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=phsowGaPiekVpYBgX3amNuQCjLtkOZ4qZTLOaJifUXP2IVTXRN7uqgqzNBR0TfJ6UsV0PaZNjU8PJwM/+km+oncRIr8Nz+rHuNMTJ/xrrlPbk66h4kRaw+H+q/c8j4Seh30NY/SCXWOxJf6PllDeVD1dx+mR76UmiJ3xxRZElCA= Received: by 10.37.22.49 with SMTP id z49mr2510272nzi; Fri, 02 Jun 2006 08:40:28 -0700 (PDT) Received: by 10.36.118.2 with HTTP; Fri, 2 Jun 2006 08:40:28 -0700 (PDT) Message-ID: Date: Sat, 3 Jun 2006 03:40:28 +1200 From: "Jonathan Roewen" To: "julien.michel@etu.univ-orleans.fr" Subject: Re: [Caml-list] adding lots of elements to a list Cc: caml-list@yquem.inria.fr In-Reply-To: <1149262126.4480592e24237@webmailetu.univ-orleans.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <1149262126.4480592e24237@webmailetu.univ-orleans.fr> X-j-chkmail-Score: MSGID : 44805BED.000 on nez-perce : j-chkmail score : X : 0/20 1 X-Miltered: at nez-perce with ID 44805BED.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; iteration:01 decr:01 printf:01 printf:01 decr:01 'list:01 structurally:01 bool:01 caml-list:01 exception:01 expression:01 argument:01 boolean:02 equation:02 boolean: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.5 required=5.0 tests=RCVD_BY_IP,TO_ADDRESS_EQ_REAL autolearn=disabled version=3.0.3 > 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