feat(haskell): add roman numerals decoder kata

This commit is contained in:
Dmitriy Pleshevskiy 2022-04-20 03:27:08 +03:00
parent 2a6c53e075
commit 8fa41c4e4c
2 changed files with 24 additions and 1 deletions

View File

@ -1,4 +1,4 @@
module Codewars.Kata.SplitStrings where
module Codewars.Kata.SplitStrings (solution) where
import qualified Data.Char as Char

View File

@ -0,0 +1,23 @@
{-# LANGUAGE NumericUnderscores #-}
module Roman (solution) where
solution :: String -> Int
solution = sum . mergeParts . map fromRoman
mergeParts :: [Int] -> [Int]
mergeParts [] = []
mergeParts [n1] = [n1]
mergeParts (n1:n2:rest)
| n1 < n2 = n2 - n1 : mergeParts rest
| otherwise = n1 : mergeParts (n2:rest)
fromRoman :: Char -> Int
fromRoman c = case c of
'I' -> 1
'V' -> 5
'X' -> 10
'L' -> 50
'C' -> 100
'D' -> 500
'M' -> 1_000
_ -> 0