From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA32567; Thu, 11 Oct 2001 19:53:02 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA32692 for ; Thu, 11 Oct 2001 19:53:01 +0200 (MET DST) Received: from ms1.surfglobal.net (eone.surfglobal.net [207.136.213.7]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f9BHqxb21584; Thu, 11 Oct 2001 19:53:00 +0200 (MET DST) Received: from vaiobambino (GILLIGAN [140.186.148.48]) by ms1.surfglobal.net with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id 4KGRN1NF; Thu, 11 Oct 2001 13:52:53 -0400 From: "Jeff Henrikson" To: "Daniel de Rauglaudre" Cc: Subject: [Caml-list] Thanks: C style for loop Date: Thu, 11 Oct 2001 14:05:51 -0400 Message-ID: <004101c1527f$5d064220$0b01a8c0@mit.edu> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Importance: Normal In-Reply-To: <20011011105839.A5235@verdot.inria.fr> Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Awesome, thanks. What an impressive tool. I've gotta learn it for myself so I can do all the things I was never able to do with the C preprocessor! Jeff Henrikson > -----Original Message----- > From: owner-caml-list@pauillac.inria.fr > [mailto:owner-caml-list@pauillac.inria.fr]On Behalf Of Daniel de > Rauglaudre > Sent: Thursday, October 11, 2001 4:59 AM > To: caml-list@inria.fr; jprevost@panasas.com > Subject: Re: [Caml-list] C style for loop > > > Hi, > > On Thu, Oct 11, 2001 at 01:47:07AM -0400, Jeff Henrikson wrote: > > > Okay, so maybe I should be more specific about what I want in a > > "C-style for loop." Its readablity merits are hopefully self > > evident. Well, unless you're a compulsive CPS addict who wishes > > even his grocery list could be written to tail recurse. . . > > > (* loop var | init val | while | expr for next val *) > > for c 0 (c<10) (c+1) do > > (* bla *) > > done; > > ------------------------------------- file cloop.ml > #load "q_MLast.cmo"; > #load "pa_extend.cmo"; > > open Pcaml; > > value gensym = > let cnt = ref 0 in > fun var -> > let x = do { incr cnt; cnt.val } in > var ^ "_gensym" ^ string_of_int x > ; > > value gen_for loc v iv wh nx e = > let loop_fun = gensym "iter" in > <:expr< > let rec $lid:loop_fun$ $lid:v$ = > if $wh$ then do { $e$; $lid:loop_fun$ $nx$ } else () > in > $lid:loop_fun$ $iv$ >> > ; > > EXTEND > expr: LEVEL "expr1" > [ [ "for"; v = LIDENT; iv = expr LEVEL "simple"; wh = expr LEVEL "simple"; > nx = expr LEVEL "simple"; "do"; e = expr; "done" -> > gen_for loc v iv wh nx e ] ] > ; > END; > ------------------------------------- > > Compilation: > $ ocamlc -pp camlp4r -I `camlp4 -where` -c cloop.ml > > Example under the toplevel: > $ ocaml -I `camlp4 -where` > Objective Caml version 3.02+7 (2001-09-29) > > # #load "camlp4o.cma";; > Camlp4 Parsing version 3.02+7 (2001-09-29) > > # #load "cloop.cmo";; > # for i = 0 to 10 do print_int i; done;; (* normal loop *) > 012345678910- : unit = () > # for c 0 (c<10) (c+1) do print_int c; done;; > 0123456789- : unit = () > # for c 0 (c<10) (c+3) do print_int c; done;; > 0369- : unit = () > > Compilation: > $ cat foo.ml > for c 0 (c<10) (c+2) do print_int c; done > $ ocamlc -pp "camlp4o ./cloop.cmo" -c foo.ml > $ > > Pretty print the resulting program: > $ camlp4o ./cloop.cmo pr_o.cmo foo.ml > let rec iter_gensym1 c = > if c < 10 then begin print_int c; iter_gensym1 (c + 2) end > in > iter_gensym1 0;; > > Hope this help. > > -- > Daniel de RAUGLAUDRE > daniel.de_rauglaudre@inria.fr > http://cristal.inria.fr/~ddr/ > ------------------- > Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ > To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr > ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr