Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Parsing and pretty printing of Roman numerals.
This module provides functions for parsing and pretty printing Roman numerals. Because the notation of Roman numerals has varied through the centuries this package allows for some customisation using a configuration that is passed to the conversion functions.
Example:
>>>
toRoman 1729 :: String
"MDCCXXIX"
>>>
fromRoman "MDCCXXIX" :: Maybe Integer
Just 1729
>>>
fromRoman "Bla" :: Maybe Integer
Nothing
Synopsis
- data NumeralConfig n
- mkNumConfig :: (Ord n, Num n) => Text -> Text -> [(Text, n)] -> NumeralConfig n
- convertTo :: (Ord n, Num n) => NumeralConfig n -> n -> Text
- convertFrom :: (Ord n, Num n) => NumeralConfig n -> Text -> Maybe n
- modernRoman :: (Ord n, Num n) => NumeralConfig n
- toRoman :: (Ord n, Num n) => n -> Text
- fromRoman :: (Ord n, Num n) => Text -> Maybe n
Types
data NumeralConfig n Source #
A configuration with which the convertTo
and convertFrom
functions can
be parameterized.
:: (Ord n, Num n) | |
=> Text | Symbol for zero |
-> Text | Symbol for one |
-> [(Text, n)] | Symbol-value table. |
-> NumeralConfig n |
Smart constructor for a NumeralConfig
.
Pretty printing
convertTo :: (Ord n, Num n) => NumeralConfig n -> n -> Text Source #
Converts a number to a Roman numeral according to the given configuration.
Parsing
convertFrom :: (Ord n, Num n) => NumeralConfig n -> Text -> Maybe n Source #
Parses a string as a Roman numeral according to the given
configuration. Result is Nothing
if the input is not a valid numeral.
Default Configurations
modernRoman :: (Ord n, Num n) => NumeralConfig n Source #
Configuration for Roman numerals as they are commonly used today. The value 0 is represented by the empty string. It can be interpreted as not writing down a number. This configuration is practically limited to the range [1..3999]. Smaller numbers will result in an empty string. Larger numbers will result in repeated use of the 'M' symbol.