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 KAA19813; Thu, 11 Oct 2001 10:58:43 +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 KAA19806 for ; Thu, 11 Oct 2001 10:58:42 +0200 (MET DST) Received: from verdot.inria.fr (verdot.inria.fr [128.93.11.7]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f9B8web03356; Thu, 11 Oct 2001 10:58:40 +0200 (MET DST) Received: (from ddr@localhost) by verdot.inria.fr (8.9.3/8.9.3) id KAA05377; Thu, 11 Oct 2001 10:58:39 +0200 Date: Thu, 11 Oct 2001 10:58:39 +0200 From: Daniel de Rauglaudre To: caml-list@inria.fr, jprevost@panasas.com Subject: Re: [Caml-list] C style for loop Message-ID: <20011011105839.A5235@verdot.inria.fr> References: <004001c15218$29c42e20$0b01a8c0@mit.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <004001c15218$29c42e20$0b01a8c0@mit.edu>; from jehenrik@yahoo.com on Thu, Oct 11, 2001 at 01:47:07AM -0400 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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