I am very new to Haskell, I have a problem, how to split given string into list of words.
example "Hello world from haskell"
-> ["Hello","world","from","haskell"]
thanks for your help
You can use Hoogle and search for example by signature. Since you want to convert a String
to a list of String
s, the signature is thus String -> [String]
. The first matches are lines :: String -> [String]
and words :: String -> [String]
. Based on the name of the function, words
is the right match.
As the documentation on words
says:
words :: String -> [String]
words
breaks a string up into a list of words, which were delimited by white space.>>> words "Lorem ipsum\ndolor" ["Lorem","ipsum","dolor"]
This thus seems to be the function you are looking for. If we run this in ghci
, we get the expected output:
Prelude> words "Hello world from haskell"
["Hello","world","from","haskell"]
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