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 VAA08371; Fri, 30 May 2003 21:15:33 +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 VAA08359 for ; Fri, 30 May 2003 21:15:32 +0200 (MET DST) Received: from escargot.exelixis.com (shaker.exelixis.com [65.209.203.254]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h4UJFVH26384 for ; Fri, 30 May 2003 21:15:31 +0200 (MET DST) Received: from quasar.ipa.nw.ru (dhcp-172-29-16-184.exelixis.com [172.29.16.184]) by escargot.exelixis.com (8.8.8/8.8.8/GNAC-GW-2.1) with ESMTP id MAA06549; Fri, 30 May 2003 12:15:27 -0700 (PDT) Message-ID: <3ED7AD61.7020103@quasar.ipa.nw.ru> Date: Fri, 30 May 2003 12:13:37 -0700 From: "Alexander V. Voinov" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030507 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Matt Gushee CC: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] Good examples for a Camlp4 beginner? References: <20030530185206.GC2087@swordfish> In-Reply-To: <20030530185206.GC2087@swordfish> Content-Type: multipart/mixed; boundary="------------050806050909010700080701" X-Spam: no; 0.00; voinov:01 quasar:01 caml-list:01 camlp:01 analogs:01 python's:01 gushee:01 all--:01 real-world:01 non-trivial:01 pcaml:01 expr:01 foldr:01 foldl:01 printf:01 X-Attachments: name="listsugar.ml" name="listsugar.ml" name="testsugar.ml" name="testsugar.ml" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk This is a multi-part message in MIME format. --------------050806050909010700080701 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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. > > > --------------050806050909010700080701 Content-Type: text/plain; name="listsugar.ml" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="listsugar.ml" 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;; --------------050806050909010700080701 Content-Type: text/plain; name="testsugar.ml" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="testsugar.ml" 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 --------------050806050909010700080701-- ------------------- 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/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners