Proposal: Add split and splitWith (trac #2048)

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


On Thu, 17 Jan 2008, Twan van Laarhoven wrote:

> Hello Haskellers,
>
> 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
>
> trac: http://hackage.haskell.org/trac/ghc/ticket/2048
> deadline: two weeks from now, January 30

The type could be more specific. We know that the result list is always
non-empty, and that the first element of each sub-list matches 'p', except
the leading sub-list. Ideally we had a list type for elements of
alternating type, like this one:
  http://darcs.haskell.org/event-list/src/Data/AlternatingList/List/Uniform.hs
 Then we could use the signature:
    splitWith :: (a -> Bool) -> [a] -> AlternatingList.T [a] a

We can simulate this type by:
    splitWith :: (a -> Bool) -> [a] -> ([a], [(a, [a])])


More information about the Libraries mailing list