Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Alexander V. Voinov" <avv@quasar.ipa.nw.ru>
To: Matt Gushee <mgushee@havenrock.com>
Cc: caml-list@pauillac.inria.fr
Subject: Re: [Caml-list] Good examples for a Camlp4 beginner?
Date: Fri, 30 May 2003 12:13:37 -0700	[thread overview]
Message-ID: <3ED7AD61.7020103@quasar.ipa.nw.ru> (raw)
In-Reply-To: <20030530185206.GC2087@swordfish>

[-- Attachment #1: Type: text/plain, Size: 807 bytes --]

Hi Matt,

See a simple extension, which adds some sugar on top of the List module 
functionality. My purpose in this case was to give close analogs to 
Python's loops over sequences (e.g. lists), to facilitate transition. An 
example is attached as well.

Alexander

Matt Gushee wrote:

>Hello, all--
>
>Over the past couple of weeks I have been learning about the various
>OCaml parsing and lexing tools, with an emphasis on Camlp4. It's
>fascinating, and I've learned a lot, but I am still having trouble
>grasping how the different components fit together. I think what I need
>now is to look at some examples of working, real-world code that use
>Camlp4 ... something non-trivial, but not enormously complex. Can anyone
>suggest a good place to start?
>
>Thanks in advance for any suggestions.
>
>  
>


[-- Attachment #2: listsugar.ml --]
[-- Type: text/plain, Size: 957 bytes --]

open Pcaml;;

EXTEND
  GLOBAL: expr;

  expr: 
    [ [ "map"; list = expr; "with"; OPT "|"; clauses = LIST1 clause SEP "|" ->
	  <:expr< List.map (fun [ $list:clauses$ ]) $list$ >> ]

    | [ "iterate"; list = expr; "with"; OPT "|"; clauses = LIST1 clause SEP "|" ->
	  <:expr< List.iter (fun [ $list:clauses$ ]) $list$ >> ]
	
    | [ "foldr"; list = expr; "from"; initval = expr; "with"; OPT "|"; 
	clauses = LIST1 clause SEP "|" ->
	  <:expr< List.fold_right (fun a b -> 
				     match (a, b) with [ $list:clauses$ ]) 
	                           $list$ $initval$ >> ]

    | [ "foldl"; list = expr; "from"; initval = expr; "with"; OPT "|"; 
	clauses = LIST1 clause SEP "|" ->
	  <:expr< List.fold_left (fun a b -> 
				     match (a, b) with [ $list:clauses$ ]) 
	                           $initval$ $list$ >> ]
    ];

  clause:
    [[ p = patt; w = OPT when_expr; "->"; e = expr -> (p, w, e)]];

  when_expr:
    [[ "when"; e = expr -> e ]];
END;;



[-- Attachment #3: testsugar.ml --]
[-- Type: text/plain, Size: 973 bytes --]

open Printf

let _ =
  let values = [1; 2; 3; 4; 5; 6; 7] in

  let vals2 = 
    map values with
      | 1 | 3 | 7 -> 10
      | v when v mod 2 == 0 -> v + 1 
      | v -> v - 1
  in

  let valfmtd = String.concat ", " (map vals2 with v -> sprintf "<%d>" v) in

  let suml, sumsquares, nelem = 
    foldl values from 0.0, 0.0, 0 with 
	(suml0, sumsq0, n), value -> 
	  let fv = float value in 
	  (suml0 +. fv, sumsq0 +. fv *. fv, n + 1) 
  in
  let mean = suml /. float nelem in
  let sdev = sqrt ((sumsquares -. float nelem *. mean *. mean) 
		   /. (float nelem -. 1.0)) in

  let sumr = 
    foldr values from 0.0 with 
	value, sum0 -> (
	  printf "found value = %d, sum0 = %5.2f\n" value sum0;
	  sum0 +. float value
	) 
  in

  printf "%s\n" valfmtd;
  printf "mean: %g, sdev: %g\n" mean sdev;
  printf "sumr: %g\n" sumr;

  iterate values with
    | 1 -> printf "one\n"
    | v when v mod 2 == 0 -> printf "%d is even\n" v
    | v -> printf "%d is odd\n" v


      

  reply	other threads:[~2003-05-30 19:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-30 18:52 Matt Gushee
2003-05-30 19:13 ` Alexander V. Voinov [this message]
2003-05-30 19:27   ` Matt Gushee
2003-05-30 20:13 ` Basile STARYNKEVITCH

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=3ED7AD61.7020103@quasar.ipa.nw.ru \
    --to=avv@quasar.ipa.nw.ru \
    --cc=caml-list@pauillac.inria.fr \
    --cc=mgushee@havenrock.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