caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: TeXitoi <texitoi+news@texitoi.eu>
To: caml-list@inria.fr
Subject: Re: While loop
Date: 31 Aug 2010 00:53:17 +0200	[thread overview]
Message-ID: <87vd6rd86a.fsf@epiano.appart> (raw)
In-Reply-To: <AANLkTimbYyvs64etwRB831q4g3aat5RD1_Rz1PjhGxXd@mail.gmail.com>

Mike Chen <mickey.chuen@gmail.com> writes:

> 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

!ele is an int, so it will only be calculated before entering the
loop, and will not be updated.

maybe you can try
  let ele () = List.nth list !i in

and s/!ele/(ele ())/g

> 	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? *)

I've only ligthly read the code, it can be something else.

-- 
Guillaume Pinot                                http://www.texitoi.eu

« Les grandes personnes ne comprennent jamais rien toutes seules, et
c'est fatigant, pour les enfants, de toujours leur donner des
explications... » -- Antoine de Saint-Exupéry, Le Petit Prince

()  ASCII ribbon campaign      -- Against HTML e-mail
/\  http://www.asciiribbon.org -- Against proprietary attachments


  reply	other threads:[~2010-08-30 22:55 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 [this message]
2010-08-30 22:57 ` [Caml-list] " Shawn Wagner
2010-08-30 23:25 ` 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=87vd6rd86a.fsf@epiano.appart \
    --to=texitoi+news@texitoi.eu \
    --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).