Why is there no splitBy in the list module?

Christian Maeder maeder at tzi.de
Wed Jul 12 06:56:09 EDT 2006


Simon Marlow schrieb:
> Would someone like to make a concrete proposal (with code!) for 2-3
> functions we could reasonably add to Data.List?

Here is my proposal that is consistent with Data.PackedString and
"lines" (i.e a final delimiter is ignored -- by extra code)

{- | The 'splitWith' function takes a predicate and splits the input
list at each element which satisfies the predicate. -}
splitWith :: (a -> Bool) -> [a] -> [[a]]
splitWith p s =
    case s of
      [] -> []
      _ -> let (l, r) = break p s in
           case r of
             _ : t@(_ : _) -> l : splitWith p t
             _ -> [l]

{- | The 'split' function splits the input list on each occurrence of
the given element. -}
split :: Eq a => a -> [a] -> [[a]]
split c = splitWith (== c)



More information about the Libraries mailing list