I've been playing with Haskell data types for the past few days, using a custom type to work with Roman numerals:
data RomanNumeral = I | IV | V | IX | X | XL | L | XC | C | CD | D | CM | M deriving (Eq, Ord, Show, Read)
stringToRomanNumeral :: String -> Maybe [RomanNumeral]
stringToRomanNumeral romString
| unRoman = Nothing
| otherwise = Just $ map read $ map (\x -> [x]) romStringUpper
where
romStringUpper = map C.toUpper romString
unRoman = any (`notElem` "MDCLXVI") romStringUpper
This works fine, but catches only 1-char numerals (so I have to calculate the value of IV, IX etc. later on).
Is there a way to read (or reads) the input string such that the returned value of Maybe [RomanNumeral] contains 2-char numerals, too? I tried dabbling with pattern matching, but I cannot seem to get the type right.