
2 Jan
2012
2 Jan
'12
5:42 a.m.
On Mon, Jan 2, 2012 at 3:14 PM, max
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"] -- Anupam