dropWhileRev

David Roundy droundy at abridgegame.org
Sun Aug 7 14:39:23 EDT 2005


On Sun, Aug 07, 2005 at 09:10:03AM -0500, Jonathan Cast wrote:
> Henning Thielemann <lemming at henning-thielemann.de> wrote:
> 
> > Is this function worth to be added to Data.List?
> > 
> > 
> > {-| Remove the longest suffix of elements satisfying the predicate.
> >     In contrast to 'reverse . dropWhile p . reverse'
> >     this works for infinite lists, too. -}
> > dropWhileRev :: (a -> Bool) -> [a] -> [a]
> > dropWhileRev p =
> >    foldr (\x xs -> if p x && null xs then [] else x:xs) []
...
> What do you mean by ``works for inifinite lists''?

(I don't know if the following is what Hennig meant, but it's what I
understood.)

It "works for infinite lists that don't have an infinite sequence of
matching elements".  Even for such problematic lists the above will work
until you hit that matching sequence.

take 10 $ dropWhileRev (/= 2) ([1..])

works.  But with the "obvious" implementation you'll *always* get failure
on infinite lists.  It even works "correctly" if you choose to define the
meaning of the function to be that it removes the longest contiguous
matching sequence that includes the end of the list.
-- 
David Roundy
http://www.darcs.net


More information about the Libraries mailing list