From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id B1E54BC57 for ; Tue, 31 Aug 2010 01:50:04 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Am8EAA/ge0xQW+UMgWdsb2JhbACgahUBARYiIr9ShTcEigk X-IronPort-AV: E=Sophos;i="4.56,295,1280700000"; d="scan'208";a="56202229" Received: from lo.gmane.org ([80.91.229.12]) by mail3-smtp-sop.national.inria.fr with ESMTP; 31 Aug 2010 01:50:04 +0200 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1OqE6z-0000JK-39 for caml-list@inria.fr; Tue, 31 Aug 2010 01:50:01 +0200 Received: from epiano.texitoi.eu ([88.164.18.122]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 31 Aug 2010 01:50:01 +0200 Received: from texitoi+news by epiano.texitoi.eu with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 31 Aug 2010 01:50:01 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: TeXitoi Subject: Re: While loop Date: 31 Aug 2010 01:49:52 +0200 Sender: texitoi@epiano.appart Message-ID: <87mxs3d5jz.fsf@epiano.appart> References: <4C7C3E01.4060507@grant-olson.net> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: epiano.texitoi.eu User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 X-Spam: no; 0.00; val:01 val:01 language's:01 discution:01 guillaume:01 prince:98 ribbon:98 campaign:98 wrote:01 rec:01 int:01 int:01 writes:01 append:02 append:02 Grant Olson 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 = > > > > # 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 = > # 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 = # 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