On Mon, 30 Aug 2010 15:43:40 -0700
Mike Chen <mickey.ch...@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
sha...@speakeasy.org

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to