Just for kicks I decided to revisit my Roman numeral routines from Euler 89.

Admittedly, my earlier Python algorithms were not the most efficient, but they got the job done. Thought it might be worth seeing if Haskell can do it better. As usual, I re-wrote some of the more tighter online routines for clarify and fed the Euler roman.txt file through it to get the same answer.

```
```import Data.Maybe
dec2num :: Integer -> String
dec2num x
| x == 0 = ""
| otherwise = b ++ dec2num (x - a)
where
(a, b) = head $ filter ((<= x) . fst)
$ zip [1000,900,500,400,100,90,50,40,10,9,5,4,1]
["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
num2dec :: String -> Integer
num2dec n = fst $ foldr (\p (t,s) -> if p >= s then (t+p,p) else (t-p,p)) (0,0)
$ map (fromJust . flip lookup numMap) n
where
numMap = zip "IVXLCDM" [1,5,10,50,100,500,1000]
main = do
raw <- readFile "roman.txt"
print $ (sum [length s|s <- lines raw]- sum [length $ dec2num $ num2dec s|s <- lines raw])

### Like this:

Like Loading...

*Related*