Reputation: 1301
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
Upvotes: 5
Views: 12285
Reputation: 432
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"]
Reference: https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-String.html#v:words
Upvotes: 7
Reputation: 476547
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"]
Upvotes: 18