[Haskell-cafe] wordsBy in the base libraries?

Neil Mitchell ndmitchell at gmail.com
Mon Oct 22 05:50:44 EDT 2007


Hi

We certainly need a function to split a list into sections. Whether it
be wordsBy, or something like linesBy, or some new split variant.

> > > wordsBy :: (a -> Bool) -> [a] -> [[a]]
> > > wordsBy p s = case dropWhile p s of
> > >   []      -> []
> > >   s':rest -> (s':w) : wordsBy p s''
> > >     where (w, s'') = break p rest

You are still over by one test. Try instead:

wordsBy :: (a -> Bool) -> [a] -> [[a]]
wordsBy p s = case dropWhile p s of
   []      -> []
   s':rest -> (s':w) : wordsBy p (drop 1 s'')
          where (w, s'') = break p rest

The supero paper (http://www-users.cs.york.ac.uk/~ndm/supero/) has a
very short explanation of this.

Thanks

Neil


More information about the Haskell-Cafe mailing list