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

Bayley, Alistair Alistair_Bayley at invescoperpetual.co.uk
Wed Jul 12 05:18:15 EDT 2006

> From: haskell-cafe-bounces at haskell.org 
> [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Donald 
> Bruce Stewart
> I vote for this, currently implemented in Data.ByteString:
>     -- | split on characters
>     split        :: Char -> String -> [String]
>     -- | split on predicate *
>     splitBy      :: (Char -> Bool) -> String -> [String]
> and
>     -- | split on a string
>     tokens       :: String -> String -> [String]
> Question over whether it should be:
>     splitBy (=='a') "aabbaca" == ["","","bb","c",""]
>   or
>     splitBy (=='a') "aabbaca" == ["bb","c"]
> I argue the second form is what people usually want.

Based on...?

Does tokens preserve empty fields, like the proposed first form of
splitBy? There is a fairly strong case for the first form when looking
at CSV file parsers e.g.

  splitBy (== ',') "a,b,,d" == ["a", "b", "", "d"]

In this case you want to correctly notice that a given field is empty.
As Christian Maeder notes, it's pretty easy to filter them out
afterwards, if that's what you want.

OTOH, if tokens does this, then I'm not concerned that splitBy doesn't.

Confidentiality Note: The information contained in this message,
and any attachments, may contain confidential and/or privileged
material. It is intended solely for the person(s) or entity to
which it is addressed. Any review, retransmission, dissemination,
or taking of any action in reliance upon this information by
persons or entities other than the intended recipient(s) is
prohibited. If you received this in error, please contact the
sender and delete the material from any computer.

More information about the Haskell-Cafe mailing list