Proposal: Add split and splitWith (trac #2048)

Henning Thielemann lemming at henning-thielemann.de
Thu Jan 17 01:53:00 EST 2008


On Thu, 17 Jan 2008, Ian Lynagh wrote:

> On Thu, Jan 17, 2008 at 12:00:37AM +0100, Twan van Laarhoven wrote:
> >
> > An often requested function is 'split', to split a list into parts
> > delimited by some separator. ByteString has the functions split and
> > splitWith for this purpose. I propose we add equivalents to Data.List:
> >
> > > split :: Eq a => a -> [a] -> [[a]]
> > > split x = splitWith (x==)
> > >
> > > splitWith :: (a -> Bool) -> [a] -> [[a]]
> > > splitWith p xs = ys : case zs of
> > >                         []   -> []
> > >                         _:ws -> splitWith p ws
> > >                   where (ys,zs) = break p xs
>
> One or the other should be changed so that these agree:
>
> *Main> split 'a' ""
> [""]
> *Main> Data.ByteString.Char8.split 'a' (Data.ByteString.Char8.pack "")
> Loading package array-0.1.0.0 ... linking ... done.
> Loading package bytestring-0.9.0.1 ... linking ... done.
> []
>
> although I couldn't say which is "right" OTTOMH...

List's split is more consistent. It always returns a non-empty list. The
number of sub-lists is the number of matching elements plus 1.


More information about the Libraries mailing list