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 concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id D7574BB81 for ; Sat, 1 Oct 2005 02:13:07 +0200 (CEST) Received: from sdmx1.scea.com (sdmx1.scea.com [160.33.44.36]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j910D63s011162 for ; Sat, 1 Oct 2005 02:13:07 +0200 Received: from edenfox.989studios.com (edenfox.989studios.com [160.33.45.60]) by sdmx1.scea.com (8.13.1/8.13.1) with ESMTP id j910D5RN017192 for ; Fri, 30 Sep 2005 17:13:05 -0700 X-Envelope-From: X-Envelope-To: Received: from slave-dog.naughtydog.com (intmail.naughtydog.com [10.15.0.2]) by edenfox.989studios.com (8.12.10/8.12.10/SCEAint-1.0) with SMTP id j910D36d009924 for ; Fri, 30 Sep 2005 17:13:04 -0700 Received: from SMTP agent by mail gateway Fri, 30 Sep 2005 17:07:40 -0800 Received: from katn-dog.naughtydog.com (engstad@katn-dog.naughtydog.com [10.0.0.90]) by slave-dog.naughtydog.com (SGI-8.12.5/8.12.5) with ESMTP id j910726316668342; Fri, 30 Sep 2005 17:07:02 -0700 (PDT) From: Pal-Kristian Engstad Organization: Naughty Dog, Inc. To: caml-list@yquem.inria.fr Subject: Re: Ant: Re: [Caml-list] Avoiding shared data Date: Fri, 30 Sep 2005 17:07:00 -0700 User-Agent: KMail/1.8.2 Cc: Oliver Bandel References: <20050926081727.GA9114@coruscant.stwing.upenn.edu> <20050926210730.55850.qmail@web26803.mail.ukl.yahoo.com> <20050930225737.GA592@first.in-berlin.de> In-Reply-To: <20050930225737.GA592@first.in-berlin.de> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200509301707.01281.pal_engstad@naughtydog.com> X-Scanned-By: MIMEDefang 2.48 on 160.33.44.36 X-Scanned-By: MIMEDefang 2.48 on 160.33.45.59 X-BorderEnvelope-To: X-Miltered: at concorde with ID 433DD492.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 avoiding:01 oliver:01 bandel:01 recursive:01 shivers:01 icfp:01 gatech:01 shivers:01 argues:01 rec:01 verbose:01 verbose:01 refactor:01 hof:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=PORN_URL_MISC autolearn=disabled version=3.0.3 On Friday 30 September 2005 03:57 pm, Oliver Bandel wrote: > On the other hand: writing mor funtional/recursive code will > make you more used to to this... I've always thought that this was a really bad argument from the ML camp. T= he=20 logic of complicated control-paths is very easily made a zillion times wors= e=20 by writing in a tail-recursive style. It is *not* a good programming practi= ce=20 to make hard-to-read code! I encourage people to read the paper by Olin Shivers: "The Anatomy of a Loo= p -=20 A story of scope and control", which was presented at ICFP 2005, and can be= =20 found at http://www.cc.gatech.edu/~shivers/papers/loop.pdf. The author argues that "Writing loops with tail-recursive function calls is= =20 the equivalent of writing them with goto=E2=80=99s." and gives an example t= hat I've=20 rewritten from Scheme-ish into OCaml-ish: let myfunc l =3D let rec loop rest result =3D match rest with=20 | [] -> List.rev result | x::xs ->=20 if xpred x then let y =3D verbose_code_using_x x in if ypred y then=20 let z =3D verbose_code_using_y y in loop xs (z_expression :: result) else loop xs result else loop xs result in loop l []=20 ;; Obviously, one would like to refactor this into HOF, but in this situation = it=20 is hard to see how one should do it. The author instead proposes to use loo= ps=20 in a monadic style, which I've again rewritten: let myfunc l =3D loop [ for x in l ; when xpred x=20 ; let y =3D verbose_code_using_x x ; when ypred y ; let z =3D verbose_code_using_y y ; save z ] The point being (syntax aside) that this code is much more readible, easier= to=20 change and easier to verify than the code given above. [Of course, Haskell= =20 has the really cool list-comprehension syntax that alleviates some of ML's= =20 problems, but still.] Thanks, PKE. =2D-=20 _ =20 \`. P=C3=A5l-Kristian Engstad, Lead Programmer, \ `| Naughty Dog, Inc., 1601 Cloverfield Blvd, 6000 North, __\ |`. Santa Monica, CA 90404, USA. (310) 633-9112.=20 / /o mailto:engstad@naughtydog.com http://www.naughtydog.com / '~ mailto:mrengstad@yahoo.com http://www.engstad.com / ,' Hang-gliding Rulez! ~'