Home > Net >  How to convert every first letter in a string of words to upper case with a map?
How to convert every first letter in a string of words to upper case with a map?

Time:11-24

For example I have the String "Hello what a nice day" it is supposed to convert to this: "Hello What A Nice Day".

I want to convert it with a map.

I have tried:

upperFirst list = map (\a -> toUpper a) . filter(\(x:sx:xs) -> isSpace x && isLower sx) list

This way I wanna check, if my current char is " " and the char behind it is in lower case. But obviously it doesn't work.

Maybe someone can help me and explain it for me.

CodePudding user response:

You can work with words :: String -> [String] and unwords :: [String] -> String to split a string into a list of strings and transfer it back. Then you only need to convert each individual word, so:

upperFirst :: String -> String
upperFirst = unwords. … . words

where I leave the -part as an exercise.

  • Related