From 11b969d16904178c9a71f1a7657e8391e6f10b4c Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 19 Apr 2022 10:50:02 +0300 Subject: [PATCH] feat(haskell): add title case --- haskell/9_title_case.hs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 haskell/9_title_case.hs diff --git a/haskell/9_title_case.hs b/haskell/9_title_case.hs new file mode 100644 index 0000000..4f369f6 --- /dev/null +++ b/haskell/9_title_case.hs @@ -0,0 +1,26 @@ +module TitleCase (titleCase) where + +import qualified Data.Char as Char + +titleCase :: String -> String -> String +titleCase minor title = + unwords $ + map toTitle (take 1 titleWords) + <> map maybeToTitle (drop 1 titleWords) + where + titleWords, minorWords :: [String] + titleWords = words . toLower $ title + minorWords = words . toLower $ minor + + maybeToTitle :: String -> String + maybeToTitle word + | word `elem` minorWords = word + | otherwise = toTitle word + +toTitle :: String -> String +toTitle t = case t of + "" -> "" + (x:xs) -> Char.toUpper x : xs + +toLower :: String -> String +toLower = map Char.toLower