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

Christian Maeder maeder at tzi.de
Wed Jul 12 06:19:57 EDT 2006


Donald Bruce Stewart schrieb:
> No parsers!

I agree,

> I vote for this, currently implemented in Data.ByteString:

from which package comes Data.ByteString?

>     -- | split on characters
>     split        :: Char -> String -> [String]

the type for lists should then be:

split :: Eq a => a -- ^ seperator
      -> [a] -- ^ list to split
      -> [[a]]

(as I proposed earlier as "splitOn")

>     -- | split on predicate *
>     splitBy      :: (Char -> Bool) -> String -> [String]

According to Data.PackedString (with splitPS and splitWithPS)
the name should be "splitWith" for consistency (or splitWithPS should be
renamed as well).

>     -- | split on a string
>     tokens       :: String -> String -> [String]

I don't think, that we need this function for lists.

> 
> 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.

Yes, the second form is needed for "words", but the first form is needed
for "lines", where one final empty element needs to be removed from your
version!

Prelude> lines "a\nb\n"
["a","b"]
Prelude> lines "a\n\nb\n\n"
["a","","b",""]

One more question is whether it should be:

     splitBy (=='a') "aabbaca" == ["","","bb","c",""]
   or
     splitBy (=='a') "aabbaca" == ["","","bb","c"]

This second form corresponds to splitPS but the first more general form
may be desirable as well.

Christian



More information about the Libraries mailing list