[Haskell-cafe] wordsBy in the base libraries?
Maxime Henrion
mux at FreeBSD.org
Mon Oct 22 05:24:38 EDT 2007
Hello all,
What do you think about having a wordsBy function in the standard
libraries? It often comes in handy.
> wordsBy :: (a -> Bool) -> [a] -> [[a]]
> wordsBy p s = case dropWhile p s of
> [] -> []
> ':rest -> (s':w) : wordsBy p s''
> where (w, s'') = break p rest
This version takes care of avoiding a redundant character check that
is present in the standard definition of the words function, originally
discovered by Neil Mitchell, see his blog post here:
http://neilmitchell.blogspot.com/2007/07/equational-reasoning-in-haskell.html
Then we can define the words function naturally:
> words :: String -> [String]
> words = wordsBy isSpace
Cheers,
Maxime
More information about the Haskell-Cafe
mailing list