Proposal: Add split and splitWith (trac #2048)

Ian Lynagh igloo at earth.li
Wed Jan 16 19:00:25 EST 2008


Hi Twan,

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...


Thanks
Ian



More information about the Libraries mailing list