user2936177
user2936177

Reputation: 11

Haskell: Integers -> Roman Numerals

So, I've got this interesting practical exercise for a problem sheet: Convert integers (less than 5000) to Roman numerals. Here's the code I've written; however, I'm having difficulty loading the script in GHCI (parse error on input `='). Any ideas?

units, tens, hundreds, thousands :: [String]
units=["I", "II", "III", "IV", "V", "VI", "VII", "IIX", "IX"]
tens=["X", "XX", "XXX", "XL", "L", "LX", "LXX", "XXC", "XC"]
hundreds=["C", "CC", "CCC", "CD", "D", "DC", "DCC", "CCM","CM"]
thousands=["M", "MM", "MMM", "MV", "V"]

combine :: (Int,Int,Int,Int) -> String
combine (0,0,0,u)   = units !! u
combine (0,0,t+1,0) = tens !! t
combine (0,0,t+1,u) = tens !! t ++ units !! u
combine (0,h+1,0,0) = hundreds !! h
combine (0,h+1,t+1,0) = hundreds !! h ++ tens !! t
combine (0,h+1,t+1,u)   = hundreds !! h ++ tens !! t ++ units !! u
combine (f+1,0,0,0) = thousands !! f
combine (f+1,h+1,0,0)   = thousands !! f ++ hundreds !! h
combine (f+1,h+1,t+1,0) = thousands !! f ++ hundreds !! h ++ tens !! t
combine (f+1,h+1,t+1,u) = thousands !! f ++ hundreds !! h ++ tens !! t ++ units !! u

Upvotes: 1

Views: 2370

Answers (3)

Redu
Redu

Reputation: 26161

A little late but just for the record I have ported my code from JS to Haskell here. I believe it is one of the most efficient Integer to Roman numeral converters. However as of now it's only fine up to 3999.

import qualified Data.Map.Lazy as M
import Data.Bool (bool)
import Data.List (unfoldr)

numerals :: M.Map Int Char
numerals = M.fromList [(0,'I'),(1,'V'),(2,'X'),(3,'L'),(4,'C'),(5,'D'),(6,'M')]

toDigits :: Int -> [Int]
toDigits = unfoldr (\x -> bool Nothing (Just (rem x 10, div x 10)) (x > 0))

getFromMap :: Int -> M.Map Int Char -> Char
getFromMap = M.findWithDefault '_'

getNumeral :: (Int,Int) -> String
getNumeral t | td == 0   = ""
             | td <  4   = replicate td $ getFromMap (2 * ti) numerals
             | td == 4   = getFromMap (2 * ti) numerals : [getFromMap (2 * ti + 1) numerals]
             | td <  9   = getFromMap (2 * ti + 1) numerals : replicate (td - 5) (getFromMap (2 * ti) numerals)
             | otherwise = getFromMap (2 * ti) numerals : [getFromMap (2 * ti + 2) numerals]
               where ti  = fst t -- indices
                     td  = snd t -- digits

dec2roman :: Int -> String
dec2roman = foldl (\r t -> getNumeral t ++ r) "" . zipWith (,) [0..] . toDigits

*Main> dec2roman 1453
"MCDLIII"

Upvotes: 0

Storm-Eyes
Storm-Eyes

Reputation: 821

I have written a module to deal with the conversions between integers and roman numbers. However, there are 3 shortcomings in my module.

  1. The max roman number that my module can deal with will not exceed than 4999, because I assumed that the largest roman unit is "M" and "MMMM" is not a valid roman number by the rules.

  2. I did not use Maybe in the function "findKey" to avoid unexpected keys since I had not mastered applicative, functor and monad very well.

  3. I have no idea on how to finish prettyRoman2, which will looking for the combinations "VIV", "LXL" and "DCD" in the string of a roman number, and replace them by "IX","XC" and "CM" respectively.

     module Roman 
    ( roman2Dec
     , dec2Roman
    ) where
    
    import Data.List (isInfixOf)
    
    -- The max number the program can deal with will not exceed than 4999
    
    romanUnits :: [(Char, Int)]
    romanUnits = [('I', 1), ('V', 5), ('X', 10), ('L', 50), ('C', 100), ('D', 500), ('M', 1000)]
    
    romanDividers :: [Int]
    romanDividers = reverse $ map snd romanUnits
    
    romanDigits :: [Char]
    romanDigits  = reverse $ map fst romanUnits
    
    -- short divide n by each of those in dividers
    shortDivide :: Int -> [Int] -> [Int]
    shortDivide n [] = []
    shortDivide n dividers = let (quotient, remainder) = n `divMod` (head dividers)
                             in quotient : shortDivide remainder (tail dividers)
    
    dec2Roman :: Int -> String
    dec2Roman n = concat $ map prettyRoman1 (zipWith (\x y -> replicate x y) (shortDivide n romanDividers) romanDigits)
    
    prettyRoman1 :: String -> String
    prettyRoman1 roman
      | roman == "IIII" = "IV"
      | roman == "XXXX" = "XL"
      | roman == "CCCC" = "CD"
      | otherwise = roman
    
    -- prettyRoman2: Replace VIV, LXL, DCD with IX, XC, and CM respectively. 
    -- After that, the dec2Roman will be modifed as dec2Roman' = prettyRoman2 dec2Roman
    prettyRoman2 :: String -> String
    prettyRoman2 = undefined
    
    findKey :: Eq a => a -> [(a, b)] -> b
    findKey key = snd . head . filter (\(k, v) -> k == key)
    
    romanValue :: Char -> Int
    romanValue c = findKey c romanUnits
    
    roman2Dec :: String -> Int
    roman2Dec [] = 0
    roman2Dec [x] = romanValue x
    roman2Dec (x:y:xs)
      | romanValue x < romanValue y  = (-1) * romanValue x  + roman2Dec (y:xs)
      | otherwise = romanValue x + roman2Dec (y:xs)
    

Upvotes: 0

fjarri
fjarri

Reputation: 9726

There are actually several syntax errors in this program (Edit: thanks to @Lukasz's edits, now there's only one syntax error). But the one you're asking about is caused by the fact that you can't just create a binding in ghci. Where in a program you write

a = 1

in ghci you must write

let a = 1

otherwise you will get the parse error on input `=' error.

I would recommend you to put your program in a file and compile it with ghc or run it with runhaskell instead of inserting lets, it'll be more convenient for future work and bugfixing.

Upvotes: 2

Related Questions