a
is a string and I would like to find all words in the string which have an upper case character.
How can I convert the string to chars?
listWordsWithUpper :: String -> [String]
listWordsWithUpper a = filter (any . (isUpper a))
Example:
listWordsWithUpper "Haskell is tHe BEST" == ["Haskell", "tHe", "BEST"]
CodePudding user response:
In Haskell, a string is a list of characters. I mean that literally. The type String
is an alias to [Char]
. So a string is already a list of characters.
The piece you're missing is the tokenization part. You've got a big string and you want to split it at each word boundary. There are more complicated tokenization functions for general solutions, but for this particular case, the built-in function words
will do just fine.
words :: String -> [String]
So we want to take a string, tokenize it, and then apply a filter so as to keep only the things which contain uppercase characters. For the second half, your filter
is close. We want to filter and keep any strings where any isUpper myString
is true. So something like this will work.
listWordsWithUpper :: String -> [String]
listWordsWithUpper = filter (any isUpper) . words