[Haskell-cafe] Re: Why is there no splitBy in the list module?
Simon Marlow
simonmarhaskell at gmail.com
Wed Jul 12 04:44:09 EDT 2006
Evan Laforge wrote:
>> >> splitBy :: (a -> Bool) -- ^ whether element is a seperator
>> >> -> [a] -- ^ list to split
>> >> -> [[a]]
>>
>> P.S. inspecting more than one element looks like an over-generalization
>> to me and should be left to parsers or regexp libs.
>
>
> It's more generally useful if you don't drop the separators from the
> output:
>
> splitSepWith f = map (dropWhile f) . splitWith f
> spaces = splitSepWith Char.isSpace
>
> But this still won't let you split on comma and spaces. Either
> regexes, or pass in a [tok] -> ([conumed], [rest]) type parser:
>
> splitWith :: ([a] -> ([a], [a])) -> [a] -> [[a]]
>
> ... but why not make it take parsec parsers and put it in a parsec
> util module or something (if it isn't already there!):
>
> splitWith (Parsec.char ',' >> Parsec.spaces)
>
> ... of course, then you might ask why not use Parsec.sepBy :) but
> maybe the "split on elt" concept is easier to learn than "write a
> whole parser".
>
> I guess the problem with the splitWith thing is that it's a slippery
> path that leads right up to full-on parsers.
Exactly, and this is why we didn't reach a concensus last time.
Would someone like to make a concrete proposal (with code!) for 2-3 functions we
could reasonably add to Data.List?
Cheers,
Simon
More information about the Haskell-Cafe
mailing list