http://okmij.org/ftp/typed-formatting/FPrintScan.html
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.
References
- The complete code with many examples. http://okmij.org/ftp/typed-formatting/PrintScan.hs
- The initial view on typed sprintf and sscanf http://okmij.org/ftp/typed-formatting/PrintScanI.txt
- The message with more explanations, posted on the Haskell mailing list on Sun, 31 Aug 2008 19:40:41 -0700 (PDT)
- data F a b where
- data PrinterParser a = PrinterParser (a -> String) (String -> Maybe (a, String))
- fmt :: (Show b, Read b) => b -> F a (b -> a)
- intp :: F a b -> (String -> a) -> b
- ints :: F a b -> String -> b -> Maybe (a, String)
- sprintf :: F String b -> b
- sscanf :: String -> F a b -> b -> Maybe a
- showread :: (Show a, Read a) => PrinterParser a
- prefix :: String -> String -> Maybe String
Documentation
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
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