0

My input looks like

abc

a
b
c

abc
abc
abc
abc

I need a function that would split it into something like

[ "abc"
, "a\nb\nc"
, "abc\nabc\nabc\nabc"]

I've tried using regexes, but

  1. I can't import Text.Regex itself
  2. Module Text.Regex.Base does not export splitStr
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
c-devhax
  • 21
  • 1

1 Answers1

0

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.