caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jeff Henrikson" <jehenrik@yahoo.com>
To: "Daniel de Rauglaudre" <daniel.de_rauglaudre@inria.fr>
Cc: <caml-list@inria.fr>
Subject: [Caml-list] Thanks: C style for loop
Date: Thu, 11 Oct 2001 14:05:51 -0400	[thread overview]
Message-ID: <004101c1527f$5d064220$0b01a8c0@mit.edu> (raw)
In-Reply-To: <20011011105839.A5235@verdot.inria.fr>

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


  reply	other threads:[~2001-10-11 17:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-11  5:47 [Caml-list] " Jeff Henrikson
2001-10-11  8:58 ` Daniel de Rauglaudre
2001-10-11 18:05   ` Jeff Henrikson [this message]
2001-10-11 12:47 ` Berke Durak
2001-10-11 13:11   ` 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='004101c1527f$5d064220$0b01a8c0@mit.edu' \
    --to=jehenrik@yahoo.com \
    --cc=caml-list@inria.fr \
    --cc=daniel.de_rauglaudre@inria.fr \
    /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).