Proposal: Add split and splitWith (trac #2048)

Christian Maeder Christian.Maeder at
Fri Jan 18 04:48:44 EST 2008

Duncan Coutts wrote:
> If everyone thinks this is the right behaviour we can certainly change
> Data.ByteString to follow. It is supposed to follow the Data.List api.

Good, lines can be best shortly expressed in terms of split as follows:

mylines s = if null (last l) then init l else l
            where l = split '\n' s

This is safe (though inefficient), because split always returns a
non-empty list (in contrast to Data.ByteString.Char8.split)!

Furthermore Data.PackedString.splitPS corresponds to lines.

Below are some test case.

Cheers Christian

import qualified Data.ByteString.Char8 as B
import qualified Data.PackedString as P

*GHCI> l
*GHCI> map (intercalate "\n" . split '\n') l
*GHCI> map (B.intercalate (B.pack "\n") . B.split '\n' . B.pack) l
*GHCI> map (unlines . lines) l
*GHCI> map lines l
*GHCI> map (lines . unlines . lines) l
*GHCI> map mylines l
*GHCI> map (P.splitPS '\n' . P.packString) l
*GHCI> map (B.split '\n' . B.pack) l
*GHCI> map (split '\n') l

More information about the Libraries mailing list