
4 Jul
2007
4 Jul
'07
8:17 p.m.
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