[Haskell-cafe] Generalized version of `words'?

Christian Maeder maeder at tzi.de
Mon Jun 13 07:34:42 EDT 2005

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 Data.PackedString are splitPS and splitWithPS.

splitPS :: Char -> PackedString -> [PackedString]
The splitPS function splits the input string on each occurrence of the
given Char.

splitWithPS :: (Char -> Bool) -> PackedString -> [PackedString]
The splitWithPS function takes a character predicate and splits the
input string at each character which satisfies the predicate.

More information about the Haskell-Cafe mailing list