[Haskell-cafe] Generalized version of `words'?
Christian Maeder
maeder at tzi.de
Mon Jun 13 06:47:46 EDT 2005
Here is my version:
-- | split list at separator elements, avoid empty sublists
splitBy :: Eq a => a -> [a] -> [[a]]
splitBy x xs = let (l, r) = break (==x) xs in
(if null l then [] else [l]) ++ (if null r then [] else splitBy x $
tail r)
-- suffix "By" usually indicates a (a -> a -> Bool) argument instead of Eq
An earlier discussion about such a function did not end in a conclusion.
There are so many variants/generalizations and no agreement for a name.
Christian
Dimitry Golubovsky wrote:
> Does there exist a generalized version of the `words' function i. e.
> one that breaks an arbitrary list into parts by an arbitrary predicate?
>
> splitAt is not what I need.
>
> I had to write my own:
>
> -- A version of words, but works with any lists on any predicate.
>
> parts pred s = case dropWhile pred s of
> [] -> []
> s' -> w : parts pred s''
> where (w, s'') = break pred s'
>
> (just by parameterizing `words' found in Data.List with a predicate
> passed as a parameter).
>
> In case such a function already exists, what is its name?
>
> In the opposite case, can such a function be added to the standard
> library? (or why didn't it exist?)
>
> Dimitry Golubovsky
> Middletown, CT
More information about the Haskell-Cafe
mailing list