From 8fa41c4e4c91723ff3e59f14fac8ed76685a9169 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Wed, 20 Apr 2022 03:27:08 +0300 Subject: [PATCH] feat(haskell): add roman numerals decoder kata --- haskell/10_split_strings.hs | 2 +- haskell/11_roman_numerals_decoder.hs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 haskell/11_roman_numerals_decoder.hs diff --git a/haskell/10_split_strings.hs b/haskell/10_split_strings.hs index 4a69b2a..a1a50e2 100644 --- a/haskell/10_split_strings.hs +++ b/haskell/10_split_strings.hs @@ -1,4 +1,4 @@ -module Codewars.Kata.SplitStrings where +module Codewars.Kata.SplitStrings (solution) where import qualified Data.Char as Char diff --git a/haskell/11_roman_numerals_decoder.hs b/haskell/11_roman_numerals_decoder.hs new file mode 100644 index 0000000..8fe6a2c --- /dev/null +++ b/haskell/11_roman_numerals_decoder.hs @@ -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