It's generally a bad idea to use regex in such cases, since it's less readable then pure and concise code, that can be used here.
For example using foldr
, the only case where we should add new string into lists of strings is the case where last seen element and current element are newline
's:
split :: FilePath -> IO [String]
split path = do
text <- readFile path
return $ foldr build [[]] (init text)
where
build c (s:ls) | s == [] || head s /= '\n' || c /= '\n' = (c:s) : ls
| otherwise = [] : tail s : ls
This code produces the aforementioned result when it is given file with aforementioned content.