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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 21119BC5A for ; Tue, 31 Aug 2010 00:57:27 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnYBAFfUe0xFEXU1nGdsb2JhbACgIUgVAQEBAQEICwgJESKwaI5ShTcEhDs X-IronPort-AV: E=Sophos;i="4.56,295,1280700000"; d="scan'208";a="58003607" Received: from mail8.sea5.speakeasy.net ([69.17.117.53]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 31 Aug 2010 00:57:26 +0200 Received: (qmail 2657 invoked from network); 30 Aug 2010 22:57:23 -0000 Received: from c-67-183-159-223.hsd1.wa.comcast.net (HELO localhost.localdomain) (shawnw@[67.183.159.223]) (envelope-sender ) by mail8.sea5.speakeasy.net (qmail-ldap-1.03) with SMTP for ; 30 Aug 2010 22:57:23 -0000 Date: Mon, 30 Aug 2010 15:57:22 -0700 From: Shawn Wagner Cc: caml-list@inria.fr Subject: Re: [Caml-list] While loop Message-ID: <20100830155722.4c6ed0b9@speakeasy.org> In-Reply-To: References: X-Mailer: Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-unknown-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; shawnw:01 val:01 shawnw:01 wrote:01 speakeasy:01 speakeasy:01 caml-list:01 int:01 int:01 append:02 caml:02 seems:03 let:03 let:03 conditional:04 On Mon, 30 Aug 2010 15:43:40 -0700 Mike Chen wrote: > Hi, > > I am a caml rookie, and I need your help. > > (* pp is a very simple function *) > # let pp list = > let newList = ref [] in > let i = ref 0 in > let ele = ref (List.nth list !i) in > while (!ele) != 5 do > newList := List.append !newList [(!ele mod 3)]; > i := !i + 1; > done; > !newList;; > val pp : int list -> int list = > > # pp [ 3; 4; 5];; > > (* it seems it goes into a forever loop, but I expect it returns [ 0; > 1]. What is wrong? *) > You never update the value of ele, and so the only way the loop conditional can ever be false is if the list you pass to the function starts with a 5. -- Shawn Wagner shawnw@speakeasy.org