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 nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id E9F52BB81 for ; Sat, 1 Oct 2005 10:26:44 +0200 (CEST) Received: from ZIVLNX17.uni-muenster.de (ZIVLNX17.UNI-MUENSTER.DE [128.176.188.79]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j918QiXf008915 for ; Sat, 1 Oct 2005 10:26:44 +0200 Received: from localhost (localhost [127.0.0.1]) by ZIVLNX17 (Postfix) with ESMTP id 6EE70E000DEF; Sat, 1 Oct 2005 10:26:08 +0200 (CEST) Received: from ZIVLNX17.uni-muenster.de ([127.0.0.1]) by localhost (ZIVLNX17 [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 04822-08; Sat, 1 Oct 2005 10:26:07 +0200 (CEST) Received: from [128.176.150.193] (VPNPOOL01-0183.UNI-MUENSTER.DE [128.176.150.193]) by ZIVLNX17 (Postfix) with ESMTP id 6B7C6E000113; Sat, 1 Oct 2005 10:26:06 +0200 (CEST) In-Reply-To: <200509301707.01281.pal_engstad@naughtydog.com> References: <20050926081727.GA9114@coruscant.stwing.upenn.edu> <20050926210730.55850.qmail@web26803.mail.ukl.yahoo.com> <20050930225737.GA592@first.in-berlin.de> <200509301707.01281.pal_engstad@naughtydog.com> Mime-Version: 1.0 (Apple Message framework v622) Content-Type: text/plain; charset=WINDOWS-1252; format=flowed Message-Id: Content-Transfer-Encoding: quoted-printable Cc: caml-list@yquem.inria.fr, Oliver Bandel From: Wolfgang Lux Subject: Re: Ant: Re: [Caml-list] Avoiding shared data Date: Sat, 1 Oct 2005 10:27:02 +0200 To: Pal-Kristian Engstad X-Mailer: Apple Mail (2.622) X-Virus-Scanned: by amavisd-new at uni-muenster.de X-Miltered: at nez-perce with ID 433E4844.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; wlux:01 uni-muenster:01 caml-list:01 avoiding:01 argues:01 rec:01 verbose:01 verbose:01 refactor:01 hof:01 transformed:01 computed:01 wrote:01 imho:01 exception: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.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Pal-Kristian Engstad wrote: > The author argues that "Writing loops with tail-recursive function=20 > calls is > the equivalent of writing them with goto=92s." and gives an example = that=20 > I've > rewritten from Scheme-ish into OCaml-ish: > > let myfunc l =3D > let rec loop rest result =3D > match rest with > | [] -> List.rev result > | x::xs -> > if xpred x then > let y =3D verbose_code_using_x x in > if ypred y then > 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 [] > ;; > > Obviously, one would like to refactor this into HOF, but in this=20 > situation it > is hard to see how one should do it. Oh come on! IMHO, most loops can easily be transformed by using map,=20 filter, fold_left, and fold_right. The example given is no exception. The loop essentially applies some complex code to every element of the list and includes the result in the output list only if a condition is satisfied that is computed along with result. This is always the structure of a=20 fold. (If the condition were independent from the result one could combine a=20= filter and a map.) Thus, a straight forward rewrite of the loop is: let myfunc l =3D let f x result =3D if xpred then let y =3D verbose_code_using_x x in if ypred y then let z =3D verbose_code_using_y y in z_expression :: result else result else result in fold_right f l [] Furthermore, I would turn the two if expressions into local functions let myfunc l =3D let g y result =3D if ypred y then let z =3D verbose_code_using_y y in z_expression :: result else result in let f x result =3D if xpred x then let y =3D verbose_code_using_x x in g y result else result in fold_right f l [] Regards Wolfgang