[Haskell-cafe] Splitting a string into chunks

Adam Turoff adam.turoff at gmail.com
Fri Jan 13 16:47:43 EST 2006

On 1/13/06, Sebastian Sylvan <sebastian.sylvan at gmail.com> wrote:
> blocks = map concat . groupBy (const (not . null)) . lines

Thanks.  That's a little more involved than I was looking for, but that
certainly looks better than pattern matching on ('\n':'\n':rest).  ;-)

For the record, lines removes the trailing newline, so a string like:



becomes ["ab", "cd"], which can interfere with processing if the whitespace
is significant.  Changing this to

   blocks = map unlines . groupBy (const (not . null)) . lines

re-adds all of the newlines, thus re-adding the significant whitespace,
while still chunking everything into blocks:   ["a\nb\n","\nc\nd\n"]

Thanks again,

-- Adam

More information about the Haskell-Cafe mailing list