English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    
Browse thread
While loop
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: TeXitoi <texitoi+news@t...>
Subject: Re: While loop
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