caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Shawn Wagner <shawnw@speakeasy.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] While loop
Date: Mon, 30 Aug 2010 15:57:22 -0700	[thread overview]
Message-ID: <20100830155722.4c6ed0b9@speakeasy.org> (raw)
In-Reply-To: <AANLkTimbYyvs64etwRB831q4g3aat5RD1_Rz1PjhGxXd@mail.gmail.com>

On Mon, 30 Aug 2010 15:43:40 -0700
Mike Chen <mickey.chuen@gmail.com> 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 = <fun>
> 
> # 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


  parent reply	other threads:[~2010-08-30 22:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-30 22:43 Mike Chen
2010-08-30 22:53 ` TeXitoi
2010-08-30 22:57 ` Shawn Wagner [this message]
2010-08-30 23:25 ` [Caml-list] " Grant Olson
2010-08-30 23:49   ` TeXitoi
2010-08-31  0:17     ` [Caml-list] " Grant Olson

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=20100830155722.4c6ed0b9@speakeasy.org \
    --to=shawnw@speakeasy.org \
    --cc=caml-list@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).