[Haskell-cafe] Foldr tutorial, Inspired by Getting a Fix from a Fold

Donald Bruce Stewart dons at cse.unsw.edu.au
Mon Feb 12 04:38:38 EST 2007


pixel:
> Chris Moline <evilantleredthing at yahoo.ca> writes:
> 
> > dropWhile p = foldr (\x l' -> if p x then l' else x:l') []
> 
> invalid:  dropWhile (< 5) [1, 10, 1]  should return [10, 1]

Prelude Test.QuickCheck Text.Show.Functions> quickCheck $ \p xs -> dropWhile p xs == foldr (\x l' -> if p x then l' else x:l') [] (xs :: [Int])

Falsifiable, after 4 tests:
<function>
[-1,-3,1]


If in doubt, do a quick check!

-- Don


More information about the Haskell-Cafe mailing list