mykatas/haskell/9_title_case.hs

25 lines
698 B
Haskell

module TitleCase (titleCase) where
import qualified Data.Char as Char
titleCase :: String -> String -> String
titleCase minor = unwords . process . words . toLower
where
process :: [String] -> [String]
process [] = []
process (head:tail) = toTitle head : map maybeToTitle tail
lowerMinorWords :: [String]
lowerMinorWords = words $ toLower minor
maybeToTitle :: String -> String
maybeToTitle word
| word `elem` lowerMinorWords = word
| otherwise = toTitle word
toTitle :: String -> String
toTitle [] = []
toTitle (head:tail) = Char.toUpper head : toLower tail
toLower :: String -> String
toLower = map Char.toLower