user2878641
user2878641

Reputation:

Splitting a String in Haskell

I want to split a String in Haskell.

My inicial String would look something like

["Split a String in Haskell"]

and my expected output would be:

["Split","a","String","in","Haskell"].

From what i've seen, words and lines don't work here, because i have the type [String] instead of just String.

I've tried Data.List.Split, but no luck there either.

Upvotes: 0

Views: 354

Answers (1)

effectfully
effectfully

Reputation: 12715

import Data.List

split = (>>= words)

main = print $ split ["Split a String in Haskell"]

map words makes [["Split","a","String","in","Haskell"]] from ["Split a String in Haskell"], and concat makes [x] from [[x]]. And concat (map f xs) is equal to xs >>= f. And h xs = xs >>= f is equal to h = (>>= f).

Another way, more simple would be

split = words . head

Upvotes: 1

Related Questions