[Haskell-cafe] Re: Why is there no splitBy in the list module?
Evan Laforge
qdunkan at gmail.com
Tue Jul 11 16:00:56 EDT 2006
> >> 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. Python gets by nicely
with a split on 1 or more spaces and a split on a constant string, so
maybe those are a good compromise between generality and simplicity.
More information about the Haskell-Cafe
mailing list