Prelude function suggestions
simonmar at microsoft.com
Thu Jul 29 05:48:08 EDT 2004
-- | Splits a list into components delimited by separators, where the
-- predicate returns True for a separator element. The resulting
-- components do not contain the separators. Two adjacent separators
-- result in an empty component in the output. eg.
-- > split (=='a') "aabbaca"
split :: (a -> Bool) -> [a] -> [[a]]
split p s = case rest of
 -> [chunk]
_:rest -> chunk : split p rest
where (chunk, rest) = break p s
-- | Like 'split', except that sequences of adjacent separators are
-- treated as a single separator. eg.
-- > tokens (=='a') "aabbaca"
tokens :: (a -> Bool) -> [a] -> [[a]]
tokens p = filter (not.null) . split p
'split' is the same as Python's split. There is already a precedent for
using short names in the Data.List module so I think 'split' is the
right choice, and 'tokens' is fairly descriptive.
Then we have
words = tokens isSpace
which is quite nice. Unfortunately
lines /= split (=='\n')
because lines strips off blank lines at the end. So perhaps we do need
the third version.
More information about the Libraries