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 2FB04BB81 for ; Sat, 1 Oct 2005 23:50:46 +0200 (CEST) Received: from web26808.mail.ukl.yahoo.com (web26808.mail.ukl.yahoo.com [217.146.176.84]) by concorde.inria.fr (8.13.0/8.13.0) with SMTP id j91LojpX010160 for ; Sat, 1 Oct 2005 23:50:45 +0200 Received: (qmail 89915 invoked by uid 60001); 1 Oct 2005 21:50:45 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.de; h=Message-ID:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=gHIfH6xxkWOaOGl1xtw+oZwEaStXqQD/S+d1H52g8I8n1MOiKLv6JTTFo/rMPSs4mjsXEyKqQGHXFl0djadDWAmlusrJyZOUBpZcO95+lJeelYPCmZb/RN8WUP+UW8S4SJzwntU5koQa9qEPWItt0URuYuKpJsgjhE6kFnrl9mE= ; Message-ID: <20051001215045.89913.qmail@web26808.mail.ukl.yahoo.com> Received: from [83.180.78.172] by web26808.mail.ukl.yahoo.com via HTTP; Sat, 01 Oct 2005 23:50:45 CEST Date: Sat, 1 Oct 2005 23:50:45 +0200 (CEST) From: Martin Chabr Subject: Ant: Re: Ant: Re: [Caml-list] Avoiding shared data To: Wolfgang Lux Cc: caml-list@yquem.inria.fr In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Miltered: at concorde with ID 433F04B5.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 avoiding:01 transformed:01 wlux:01 uni-muenster:01 argues:01 rec:01 verbose:01 verbose:01 refactor:01 hof:01 transformed:01 computed:01 caml-list:01 beginner's: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.4 required=5.0 tests=DNS_FROM_RFC_ABUSE autolearn=disabled version=3.0.3 Hello Wolfgang, you are quite right, most loops can easily be transformed by using map, filter, fold_left or fold_right. I just tend to forget about it or find it uneasy if the problem is more complex. It may be just a matter of habit. Regards, Martin --- Wolfgang Lux schrieb: > Pal-Kristian Engstad wrote: > > > The author argues that "Writing loops with > tail-recursive function > > calls is > > the equivalent of writing them with goto’s." and > gives an example that > > I've > > rewritten from Scheme-ish into OCaml-ish: > > > > let myfunc l = > > let rec loop rest result = > > match rest with > > | [] -> List.rev result > > | x::xs -> > > if xpred x then > > let y = verbose_code_using_x x in > > if ypred y then > > let z = 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 > > situation it > > is hard to see how one should do it. > > Oh come on! IMHO, most loops can easily be > transformed by using map, > 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 > fold. > (If the condition were independent from the result > one could combine a > filter > and a map.) Thus, a straight forward rewrite of the > loop is: > > let myfunc l = > let f x result = > if xpred then > let y = verbose_code_using_x x in > if ypred y then > let z = 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 = > let g y result = > if ypred y then > let z = verbose_code_using_y y in > z_expression :: result > else > result > in let f x result = > if xpred x then > let y = verbose_code_using_x x in g y > result > else > result > in fold_right f l [] > > Regards > Wolfgang > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: > http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > ___________________________________________________________ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de