liboleg-2010.1.7.1: An evolving collection of Oleg Kiselyov's Haskell modules



The initial view to the typed sprintf and sscanf This code defines a simple domain-specific language of string patterns and demonstrates two interpreters of the language: for building strings (sprintf) and parsing strings (sscanf). This code thus solves the problem of typed printf/scanf sharing the same format string posed by Chung-chieh Shan.

Version: The current version is 1.1, Aug 31, 2008.




data F a b whereSource


FLit :: String -> F a a 
FInt :: F a (Int -> a) 
FChr :: F a (Char -> a) 
FPP :: PrinterParser b -> F a (b -> a) 
:^ :: F b c -> F a b -> F a c 

data PrinterParser a Source

Printerparsers (injectionprojection pairs)


PrinterParser (a -> String) (String -> Maybe (a, String)) 

fmt :: (Show b, Read b) => b -> F a (b -> a)Source

a bit of syntactic sugar to avoid hitting the Shift key too many a time

intp :: F a b -> (String -> a) -> bSource

The interpreter for printf It implements Asai's accumulator-less alternative to Danvy's functional unparsing

ints :: F a b -> String -> b -> Maybe (a, String)Source

The interpreter for scanf

sscanf :: String -> F a b -> b -> Maybe aSource

showread :: (Show a, Read a) => PrinterParser aSource

The format specification is first-class. One can build format specification incrementally This is not the case with OCaml's printf/scanf (where the format specification has a weird typing and is not first class).

Primitive Printer/parsers

prefix :: String -> String -> Maybe StringSource

A better prefixOf function prefix patt str --> Just str' if the String patt is the prefix of String str. The result str' is str with patt removed Otherwise, the result is Nothing