[Haskell-cafe] folds with escapes

Stefan O'Rear stefanor at cox.net
Wed Jul 4 20:17:26 EDT 2007


On Wed, Jul 04, 2007 at 05:08:01PM -0700, Michael Vanier wrote:
> That's cool -- good point.  takeWhile is also trivially defined in terms of 
> foldr:
>
> > takeWhile p = foldr (\x r -> if p x then x:r else []) []
>
> Can you do dropWhile in terms of foldr?  I don't see how.

dropWhile cannot be expressed (with full sharing semantics) in terms of
foldr alone, but it can be done nicely as a so-called paramorphism using
foldr and tails.

dropWhile p = foldr (\l cont -> case l of { (x:xs) | p x -> cont ; _ -> l }) [] . tails

Stefan


More information about the Haskell-Cafe mailing list