Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell String to List String split by whitespace

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

like image 513
Gusti Arya Avatar asked Jul 22 '19 07:07

Gusti Arya


1 Answers

You can use Hoogle and search for example by signature. Since you want to convert a String to a list of Strings, 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"]
like image 113
Willem Van Onsem Avatar answered Oct 12 '22 22:10

Willem Van Onsem