I am trying to find the words that start with a particular letter from a list of strings. The user will input the list of words and the starting letter. For example, something like:
"table pencil chair desk pen" "p"
So the words that start with p
should be displayed, in this case pen
and pencil
. My first step is to split the string into a list of strings using the words
function. Then how do I find what letter each word starts with? The type of the function will look like:
--------------Find words Starting with a given letter------------------
findWords :: String -> Char -> [String]
You use filter
,
foo string = filter startsWithP (words string)
then you need to define
startsWithP :: String -> Bool
More useful will be the generic variant
startsWith :: String -> Char -> Bool
to be used like "foo" `startsWith` 'f'
.
Hint #1: in Haskell a String
is defined as a list of Chars
, so all the list functions are available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With