Text.RomanNumerals
Description
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. Exceptions are dealt with by wrapping the results of conversions in the error monad.
- data (Ord n, Num n) => NumeralConfig n = NC {}
- modernRoman :: (Ord n, Num n) => NumeralConfig n
- simpleRoman :: (Ord n, Num n) => NumeralConfig n
- convertTo :: (Ord n, Num n, MonadError String m) => NumeralConfig n -> n -> m String
- unsafeConvertTo :: (Ord n, Num n) => NumeralConfig n -> n -> String
- toRoman :: (Ord n, Num n, MonadError String m) => n -> m String
- unsafeToRoman :: (Ord n, Num n) => n -> String
- convertFrom :: (Ord n, Num n, MonadError String m) => NumeralConfig n -> String -> m n
- fromRoman :: (Ord n, Num n, MonadError String m) => String -> m n
- unsafeConvertFrom :: (Ord n, Num n) => NumeralConfig n -> String -> n
- unsafeFromRoman :: (Ord n, Num n) => String -> n
Types
data (Ord n, Num n) => NumeralConfig n Source
A configuration with which the convertTo
and convertFrom
functions can be parameterized.
Constructors
NC | |
Fields
|
Sample configurations
modernRoman :: (Ord n, Num n) => NumeralConfig nSource
Configuration for Roman numerals as they are commenly used today. The value 0 is represented by the empty string. It can be interpreted as not writing down a number. This configuration is limited to the range [1..3999]. Larger numbers can be represented using Roman numerals but you will need notations that are hard or impossible to express using strings.
simpleRoman :: (Ord n, Num n) => NumeralConfig nSource
Configuration for Roman numerals that do not use the rule that a lower rank symbol can be placed before a higher rank symbol to denote the difference between them. Thus a numeral like IV will not be accepted or generated by this configuration.
Pretty printing
convertTo :: (Ord n, Num n, MonadError String m) => NumeralConfig n -> n -> m StringSource
Converts a number to a Roman numeral according to the given configuration. Numbers which are out of bounds will cause exceptions to be thrown. An exception will also be raised if no representation is possible with the given configuration. If the value of any symbol in the configuration is equal to 0 or a symbol is the empty string this function is undefined.
unsafeConvertTo :: (Ord n, Num n) => NumeralConfig n -> n -> StringSource
Like convertTo
, but exceptions are promoted to errors.
toRoman :: (Ord n, Num n, MonadError String m) => n -> m StringSource
Converts a number to a modern Roman numeral. See convertTo
for
possible exceptions.
unsafeToRoman :: (Ord n, Num n) => n -> StringSource
Like toRoman
, but exceptions are promoted to errors.
Parsing
convertFrom :: (Ord n, Num n, MonadError String m) => NumeralConfig n -> String -> m nSource
Parses a string as a Roman numeral according to the given configuration. An exception will be raised if the input is not a valid numeral.
fromRoman :: (Ord n, Num n, MonadError String m) => String -> m nSource
Parses a string as a modern Roman numeral. See convertFrom
for
possible exceptions.
unsafeConvertFrom :: (Ord n, Num n) => NumeralConfig n -> String -> nSource
Like convertFrom
, but exceptions are promoted to errors.