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
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to