6

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

1
  • 6
    words "Hello world from Haskell" Commented Jul 22, 2019 at 7:49

2 Answers 2

20

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"]
Sign up to request clarification or add additional context in comments.

Comments

7
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.