[Haskell-cafe] Re: Why is there no splitBy in the list module?

John Meacham john at repetae.net
Mon Jul 10 18:59:10 EDT 2006


On Mon, Jul 10, 2006 at 02:26:23PM +0200, Christian Maeder wrote:
> Our current (special) version is:
> 
> {- |
>   A function inspired by the perl function split. A list is splitted
>   on a seperator element in smaller non-empty lists.
>   The seperator element is dropped from the resulting list.
> -}
> splitOn :: Eq a => a -- ^ seperator
>         -> [a] -- ^ list to split
>         -> [[a]]
> splitOn x xs = let (l, r) = break (==x) xs in
>     (if null l then [] else [l]) ++ (if null r then [] else splitOn x $
> tail r)

just a note: I find splitBy a much nicer routine to provide.

> splitBy :: (a -> Bool) -- ^ whether char is a seperator
>         -> [a] -- ^ list to split
>         -> [[a]]

        John

-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-Cafe mailing list