caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* While loop
@ 2010-08-30 22:43 Mike Chen
  2010-08-30 22:53 ` TeXitoi
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Mike Chen @ 2010-08-30 22:43 UTC (permalink / raw)
  To: caml-list

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

Thanks,
Mike


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: While loop
  2010-08-30 22:43 While loop Mike Chen
@ 2010-08-30 22:53 ` TeXitoi
  2010-08-30 22:57 ` [Caml-list] " Shawn Wagner
  2010-08-30 23:25 ` Grant Olson
  2 siblings, 0 replies; 6+ messages in thread
From: TeXitoi @ 2010-08-30 22:53 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] While loop
  2010-08-30 22:43 While loop Mike Chen
  2010-08-30 22:53 ` TeXitoi
@ 2010-08-30 22:57 ` Shawn Wagner
  2010-08-30 23:25 ` Grant Olson
  2 siblings, 0 replies; 6+ messages in thread
From: Shawn Wagner @ 2010-08-30 22:57 UTC (permalink / raw)
  Cc: caml-list

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] While loop
  2010-08-30 22:43 While loop Mike Chen
  2010-08-30 22:53 ` TeXitoi
  2010-08-30 22:57 ` [Caml-list] " Shawn Wagner
@ 2010-08-30 23:25 ` Grant Olson
  2010-08-30 23:49   ` TeXitoi
  2 siblings, 1 reply; 6+ messages in thread
From: Grant Olson @ 2010-08-30 23:25 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1821 bytes --]

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 = <fun>
> 
> # 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 = <fun>
# 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."


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 559 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: While loop
  2010-08-30 23:25 ` Grant Olson
@ 2010-08-30 23:49   ` TeXitoi
  2010-08-31  0:17     ` [Caml-list] " Grant Olson
  0 siblings, 1 reply; 6+ messages in thread
From: TeXitoi @ 2010-08-30 23:49 UTC (permalink / raw)
  To: caml-list

Grant Olson <kgo@grant-olson.net> writes:

> 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 = <fun>
> > 
> > # pp [ 3; 4; 5];;
> 
> # 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 = <fun>
> # 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.

Can't resist...

The same thing as yours (using patern matching), and should be more
effective (using List.append in a loop to add one element at the end
is not a good idea for effectiveness) :

# let rec pp = function
    (* if a 5 or the empty list, return the empty list *)
    | 5 :: _ | [] -> []
    (* else mod 3 and recurse on the rest of the list *)
    | x :: xs -> x mod 3 :: pp xs;;
val pp : int list -> int list = <fun>
# pp [3; 4; 5];;
- : int list = [0; 1]

But maybe the beginner list is more appropriate for this kind of
discution.

-- 
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Re: While loop
  2010-08-30 23:49   ` TeXitoi
@ 2010-08-31  0:17     ` Grant Olson
  0 siblings, 0 replies; 6+ messages in thread
From: Grant Olson @ 2010-08-31  0:17 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 323 bytes --]

On 8/30/10 7:49 PM, TeXitoi wrote:
> 
> But maybe the beginner list is more appropriate for this kind of
> discution.
> 

Sorry, I didn't even realize there was a beginner's list given the low
volume on the main list...

-- 
Grant

"I am gravely disappointed. Again you have made me unleash my dogs of war."


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 559 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-08-31  0:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-30 22:43 While loop Mike Chen
2010-08-30 22:53 ` TeXitoi
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

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).