Re: ocaml comfort, where is stringOfCharList?

From: Fritz Heinrichmeyer (
Date: Wed Mar 26 1997 - 12:52:22 MET

Date: Wed, 26 Mar 1997 12:52:22 +0100
Message-Id: <>
From: Fritz Heinrichmeyer <>
Subject: Re: ocaml comfort, where is stringOfCharList?
In-Reply-To: <>

This is a repost without MIME structure:

The last days i tried to build a HTML-Posting parser in caml. It was
hard for a beginner used to do such things in scheme. The script below
includes also a small (now untested) tester. I wrote a simple
stringOfList function myself. In scheme i would have written only
(apply string thelist).

Did i miss something? Some examples for the use of the Stream-library
would be helpful, especially for "peek" and "junk".

The parser looks pretty short now, but it was hard work for me. I would
appreciate comments on the style. Could it be shorter?

Is it possible to create an mli file for a module build by a
functor as elegant as the implementation itself?

Now here is

--------------- snip -------------------

type token =
 Equal | Ampersand | Symbol of string | Value of string

open StringOfList
open StringMap

let charToToken=
  let sol = stringOfList
  let rec wordr st =
          [<''&';rest=(wordr [])>] -> [<'(Value (sol st));'Ampersand ;rest>]
  | [<''=';rest=(wordr [])>] -> [<'(Symbol (sol st));'Equal;rest>]
  | [<'' '|'\n'|'\t';rest=(wordr st)>] -> rest
  | [<'c;rest=(wordr (c::st))>] -> rest
  | [< >] -> [<'Value(sol st)>]

  in wordr []

let postparser x =
let rec ppr st =
      [<'Symbol x;'Equal;'Value y;
        rest = (ppr (StringMap.add key: x data: y st ))>] -> rest
| [<'Ampersand;'Symbol x;'Equal;'Value y;
    rest = (ppr (StringMap.add key: x data: y st ))>] -> rest
| [< >] -> st
in ppr StringMap.empty (charToToken x)

(* a small untested (3-26-97) tester
   (fun key: a data: b ->(Printf.printf "sym=%s,val=%s\n" a b))
   (postparser (Stream.of_string " ab=1&cd=2&ef=3&gh=4 "))

------------- snip ---------------------------


This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:10 MET