caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: TeXitoi <texitoi+news@texitoi.eu>
To: caml-list@inria.fr
Subject: Re: While loop
Date: 31 Aug 2010 01:49:52 +0200	[thread overview]
Message-ID: <87mxs3d5jz.fsf@epiano.appart> (raw)
In-Reply-To: <4C7C3E01.4060507@grant-olson.net>

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


  reply	other threads:[~2010-08-30 23:50 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
2010-08-30 23:49   ` TeXitoi [this message]
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=87mxs3d5jz.fsf@epiano.appart \
    --to=texitoi+news@texitoi.eu \
    --cc=caml-list@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).