On 8/30/10 6:43 PM, 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? *) > > Thanks, > Mike > Welcome. I hope you're enjoying the language. Shawn's right. You'd need to update the ele ref in your loop for the code to terminate. Might I suggest you also look into higher order functions? It's much more natural to use these to process lists in a functional language. You can get pretty far just with map, filter, reduce, and lambdas. The following code doesn't do exactly what yours does, but it does strip out all fives and perform 'mod 3' on the remaining numbers. It's a lot shorter, easier to read and understand, and more idiomatic caml. # let pp list = let list2 = List.filter (fun x -> x != 5) list in let list3 = List.map (fun x -> x mod 3) list2 in list3;; val pp : int list -> int list = # pp [3; 4; 5];; - : int list = [0; 1] # Alternately, if you really want to abort the processing at the first 5, you could use pattern-matching to create a new list. I don't want to overwhelm you with too much code if you're just getting started, but you should look into it. But be warned, once you're familiar with pattern-matching, most language's case and if statements will seem painfully crippled. -- Grant "I am gravely disappointed. Again you have made me unleash my dogs of war."