roman-numerals-0.5.1.5: Parsing and pretty printing of Roman numerals

Safe Haskell | Safe
Language | Haskell98

Text.Numeral.Roman

Contents

- Types
- Pretty printing
- Parsing
- Default Configurations
- Utility

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.

Example:

      > toRoman 1729 ∷ String
      "MDCCXXIX"

      > fromRoman "MDCCXXIX" ∷ Maybe Integer
      Just 1729

      > convertTo simpleRoman 1729 ∷ String
      "MDCCXXVIIII"

      > fromRoman "Bla" ∷ Maybe Integer
      Nothing

Synopsis

- data NumeralConfig s n
- mkNumConfig :: (Ord n, Num n) => s -> s -> [(s, n)] -> NumeralConfig s n
- convertTo :: Monoid s => Ord n => Num n => NumeralConfig s n -> n -> s
- class StripPrefix s where
- convertFrom :: (Monoid s, StripPrefix s, Eq s, Ord n, Num n) => NumeralConfig s n -> s -> Maybe n
- modernRoman :: (IsString s, Ord n, Num n) => NumeralConfig s n
- simpleRoman :: (IsString s, Ord n, Num n) => NumeralConfig s n
- toRoman :: (IsString s, Monoid s, Ord n, Num n) => n -> s
- fromRoman :: (IsString s, Monoid s, StripPrefix s, Eq s, Ord n, Num n) => s -> Maybe n

# Types

data NumeralConfig s n #

A configuration with which the `convertTo` and `convertFrom` functions can be parameterized.

mkNumConfig #

Arguments

:: (Ord n, Num n) |
=> s | Symbol for zero
-> s | Symbol for one
-> [(s, n)] | Symbol-value table.
-> NumeralConfig s n |

Smart constructor for a `NumeralConfig`.

# Pretty printing

convertTo :: Monoid s => Ord n => Num n => NumeralConfig s n -> n -> s #

Converts a number to a Roman numeral according to the given configuration.

# Parsing

class StripPrefix s where #

Class used to overload the input of the parsing functions.

Minimal complete definition

spNull, spStripPrefix

Methods

spNull :: s -> Bool #

spStripPrefix :: s -> s -> Maybe s #

Instances

StripPrefix ByteString # |
Methods spNull :: ByteString -> Bool # spStripPrefix :: ByteString -> ByteString -> Maybe ByteString #
StripPrefix ByteString # |
Methods spNull :: ByteString -> Bool # spStripPrefix :: ByteString -> ByteString -> Maybe ByteString #
StripPrefix Text # |
Methods spNull :: Text -> Bool # spStripPrefix :: Text -> Text -> Maybe Text #
StripPrefix Text # |
Methods spNull :: Text -> Bool # spStripPrefix :: Text -> Text -> Maybe Text #
Eq α => StripPrefix [α] # |
Methods spNull :: [α] -> Bool # spStripPrefix :: [α] -> [α] -> Maybe [α] #

convertFrom :: (Monoid s, StripPrefix s, Eq s, Ord n, Num n) => NumeralConfig s n -> s -> Maybe n #

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 :: (IsString s, Ord n, Num n) => NumeralConfig s n #

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.

simpleRoman :: (IsString s, Ord n, Num n) => NumeralConfig s n #

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. 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.

# Utility

toRoman :: (IsString s, Monoid s, Ord n, Num n) => n -> s #

Converts a number to a modern Roman numeral.

fromRoman :: (IsString s, Monoid s, StripPrefix s, Eq s, Ord n, Num n) => s -> Maybe n #

Parses a string as a modern Roman numeral.