Im learning haskell and I got a problem.
The type must be: sentences :: [String] -> [String]
I want to convert strings into a sentence
["something","","Asd dsa abc","hello world..",""]
to look like this: ["Something.","Asd dsa abc.","Hello world..."]
And I want to use a higher-order function like map.
I just cant figure out how to make this.
I managed to work with a single string:
import Data.Char
sentences :: String -> String
sentences [] = []
sentences (a:as) = (( toUpper a):as) ++ "."
So I get from this:
sentences "sas das asd"
this: "Sas das asd."
I hope someone can help me with this problem.
Thanks for your help!
Edit: Thanks for your help now it looks like this:
import Data.Char
sentences :: [String] -> [String]
sentence (a:as) = ((toUpper a):as)++['.']
sentences = map sentence
But i dont know where to put the filter
Your function coupled with map gets you half of the way, but it does not remove the empty strings from your list of strings. You can do this with filter, so in total
Note that the core of
sentences(plural) is simply the mapping ofsentence(singular) over your list of strings. The filter is only there to remove the empty strings. Without this requirement, it would simply besentences ss = map sentence ss.Now you can call
sentenceswith your list of strings to have each element transformed, except the empty strings that are removed byfilterIn general, if you have a function
foothat transformsbarintobaz, you can usemap footo transform[bar]into[baz]filter, likemap, is a higher order function which, given a predicate function and a list, returns a list consisting of the elements for which the predicate isTrue. In this case, we give the predicate function(/=""), which isTruefor all strings that are not empty.