dropWhileRev

Jonathan Cast jcast at ou.edu
Sun Aug 7 15:20:05 EDT 2005


David Roundy <droundy at abridgegame.org> wrote:
> 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.

Ah.  dropWhileRev p xn is not  what I would expect (I would expect (take
n xn where n = max n. forall i  >= n. p (xn !!  i))), but it is the most
defined computable approximation to it.

Jon Cast


More information about the Libraries mailing list