问题
I'm trying to split a list in Haskell. As to my knowledge, the easiest way to do this is with splitOn
, but this function requires Data.List.Split
, so I tried to run import Data.List.Split
in Prelude. However, I got the following error:
Could not find module Data.List.Split
Simply importing Data.List
does work, however.
What could I do to solve this? Or, even better: is there an easy, built-in alternative to split lists?
回答1:
To split a String
on arbitrary white space (e.g. any Char c
where Data.Char.isSpace c
is True
), use words
:
-- words :: String -> [String]
ghci> words "Hello World, I'm a string \n example \r\t with white space"
["Hello","World,","I'm","a","string","example","with","white","space"]
No need for additional imports, since words is part of the Prelude
.
回答2:
Data.List.Split
is not in the base I think you'll have to install split
Update
after the clarification in the comments that only splitting on whitespace is required - use words
/lines
to your need - see also @Zeta's answer.
来源:https://stackoverflow.com/questions/34175173/haskell-could-not-find-module-data-list-split