Monday, January 02, 2012

Basic Haskell - Splitting Strings by newline "\n" *only* (not "\r", "\r\n" etc.)

Based on a quick reply I posted to the Haskell mailing list recently -


On Mon, Jan 2, 2012 at 3:14 PM, max wrote:
> I want to write a function whose behavior is as follows:
>
> foo "string1\nstring2\r\nstring3\nstring4" = ["string1",
> "string2\r\nstring3", "string4"]
>
> Note the sequence "\r\n", which is ignored. How can I do this?

Here's a simple way (may not be the most efficient) -

import Data.List (isSuffixOf)
split = reverse . foldl f [] . lines
 where
   f [] w = [w]
   f (x:xs) w = if "\r" `isSuffixOf` x then ((x++"\n"++w):xs) else (w:x:xs)

Testing -

ghci> split "ab\r\ncd\nefgh\nhijk"
["ab\r\ncd","efgh","hijk"]