
22 Oct
2007
22 Oct
'07
9:24 a.m.
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.htm... Then we can define the words function naturally:
words :: String -> [String] words = wordsBy isSpace
Cheers, Maxime