caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr>
To: caml-list@inria.fr, jprevost@panasas.com
Subject: Re: [Caml-list] C style for loop
Date: Thu, 11 Oct 2001 10:58:39 +0200	[thread overview]
Message-ID: <20011011105839.A5235@verdot.inria.fr> (raw)
In-Reply-To: <004001c15218$29c42e20$0b01a8c0@mit.edu>; from jehenrik@yahoo.com on Thu, Oct 11, 2001 at 01:47:07AM -0400

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


  reply	other threads:[~2001-10-11  8:58 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-11  5:47 Jeff Henrikson
2001-10-11  8:58 ` Daniel de Rauglaudre [this message]
2001-10-11 18:05   ` [Caml-list] Thanks: " Jeff Henrikson
2001-10-11 12:47 ` [Caml-list] " Berke Durak
2001-10-11 13:11   ` Bruce Hoult
2001-10-11 13:34 Damien Doligez
2001-10-11 13:43 ` Bruce Hoult

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20011011105839.A5235@verdot.inria.fr \
    --to=daniel.de_rauglaudre@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=jprevost@panasas.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).