Dan Doel <dan.d...@gmail.com> writes: > On Thursday 02 July 2009 6:36:09 am Jon Fairbairn wrote: >> check :: (MonadPlus m) => (a -> Bool) -> a -> m a >> check p a >> | p a = return a >> | otherwise = mzero > > I've often noticed the need for a similar function in conjunction with > unfoldr: > > -- This is overly general for unfoldr, but it lines up with check > stopAt :: (MonadPlus m) => (a -> Bool) -> (a -> b) -> a -> m b > stopAt p f x > | p x = mzero > | otherwise = return (f x)
Yes, I've had occasion to use something like that too, eg things similar to: reverse . unfoldr (stopAt (==0) (swap . flip divMod 10)) where swap (a,b) = (b,a) > And of course: check = flip stopAt id . not or, equally, "stopAt p f = fmap f . check (not . p)" Granted, reverse . unfoldr (fmap (swap . flip divMod 10) . check (/=0)) isn't /quite/ as nice as the first version, but I imagine one could get used to it. -- Jón Fairbairn jon.fairba...@cl.cam.ac.uk _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe