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 D908CBC57 for ; Tue, 31 Aug 2010 01:26:14 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqABAF7be0zRVdg0kGdsb2JhbACZHYdFCBUBAQEBCQkMBxEDH6Rgmm6FNwQ X-IronPort-AV: E=Sophos;i="4.56,295,1280700000"; d="asc'?scan'208";a="56201884" Received: from mail-qw0-f52.google.com ([209.85.216.52]) by mail3-smtp-sop.national.inria.fr with ESMTP; 31 Aug 2010 01:26:13 +0200 Received: by qwi4 with SMTP id 4so27700qwi.39 for ; Mon, 30 Aug 2010 16:26:10 -0700 (PDT) Received: by 10.224.37.134 with SMTP id x6mr3457906qad.172.1283210760627; Mon, 30 Aug 2010 16:26:00 -0700 (PDT) Received: from johnmudhead.local ([66.37.58.254]) by mx.google.com with ESMTPS id l13sm8958743qck.7.2010.08.30.16.25.59 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 30 Aug 2010 16:26:00 -0700 (PDT) Message-ID: <4C7C3E01.4060507@grant-olson.net> Date: Mon, 30 Aug 2010 19:25:53 -0400 From: Grant Olson User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.8) Gecko/20100802 Thunderbird/3.1.2 MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] While loop References: In-Reply-To: X-Enigmail-Version: 1.1.1 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig6703CA575FCDF241F4C28FED" X-Spam: no; 0.00; val:01 lambdas:01 val:01 language's:01 dogs:98 wrote:01 terminate:01 caml-list:01 functions:01 int:01 int:01 append:02 caml:02 caml:02 functional:02 X-Attachments: type="application/pgp-signature" name="signature.asc" name="signature.asc" This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig6703CA575FCDF241F4C28FED Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 8/30/10 6:43 PM, Mike Chen wrote: > Hi, >=20 > I am a caml rookie, and I need your help. >=20 > (* pp is a very simple function *) > # let pp list =3D > let newList =3D ref [] in > let i =3D ref 0 in > let ele =3D ref (List.nth list !i) in > while (!ele) !=3D 5 do > newList :=3D List.append !newList [(!ele mod 3)]; > i :=3D !i + 1; > done; > !newList;; > val pp : int list -> int list =3D >=20 > # pp [ 3; 4; 5];; >=20 > (* it seems it goes into a forever loop, but I expect it returns [ 0; > 1]. What is wrong? *) >=20 > Thanks, > Mike >=20 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 =3D let list2 =3D List.filter (fun x -> x !=3D 5) list in let list3 =3D List.map (fun x -> x mod 3) list2 in list3;; val pp : int list -> int list =3D # pp [3; 4; 5];; - : int list =3D [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. --=20 Grant "I am gravely disappointed. Again you have made me unleash my dogs of war= =2E" --------------enig6703CA575FCDF241F4C28FED Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.14 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJMfD4HAAoJEP5F5V2hilTWu5UH/35autiXqyhLybIxzxZpRqpB kMBXd+hXVg/L66f0llSbGxQhnfBkhCQovegJYtnntWh7MMM2rpxGQLcaBqXZIPPa nobV5jyP21rAxUQU4UZW57e2qwtpZiUeXtGyV1laaPu4RLz08HVKCuNGeXGdaZYX 6y34yhm2ca3H4AAwtuuujgihr6WgQLdPV/veO+DjLtvGMQcR47KCt5ySR1uraLbb OA9RvKzvr3QcLETwR+CWxUmNJG1F1ArL4UuP5PXU8lbkZ3lPgUuwE+mZ8Cpb5tkn YTddb5Y/zE/0I9bSStSs9i9CUIejK6K0LV3IXrGcA6cwqncXss3Q0sxyTro2TbY= =GnzA -----END PGP SIGNATURE----- --------------enig6703CA575FCDF241F4C28FED--