caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Grant Olson <kgo@grant-olson.net>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] While loop
Date: Mon, 30 Aug 2010 19:25:53 -0400	[thread overview]
Message-ID: <4C7C3E01.4060507@grant-olson.net> (raw)
In-Reply-To: <AANLkTimbYyvs64etwRB831q4g3aat5RD1_Rz1PjhGxXd@mail.gmail.com>

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

  parent reply	other threads:[~2010-08-30 23:26 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
2010-08-30 22:57 ` [Caml-list] " Shawn Wagner
2010-08-30 23:25 ` Grant Olson [this message]
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=4C7C3E01.4060507@grant-olson.net \
    --to=kgo@grant-olson.net \
    --cc=caml-list@yquem.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).