bracketOnError, while, forever
Ashley Yakeley
ashley at semantic.org
Mon Feb 7 21:30:50 EST 2005
In article <878y60v8hk.fsf at peti.cryp.to>, Peter Simons <simons at cryp.to>
wrote:
> while :: (Monad m) => m Bool -> m a -> m ()
> while cond f = cond >>= flip when (f >> while cond f)
I use this:
while :: (Monad m) => m (Maybe a) -> m [a];
while mma = do
{
ma <- mma;
case ma of
{
Just a -> do
{
as <- while mma;
return (a:as);
};
_ -> return [];
};
};
It is not tail-recursive however. I have a different function if I don't
need results:
whileDo :: (Monad m) => m Bool -> m ();
whileDo mb = mb >>= \b -> if b then whileDo mb else return ();
I also have a highly generalised "for" function, but it uses my own
Functor classes:
for :: (ExtractableFunctor f,FunctorApplyReturn m) =>
(a -> m b) -> (f a -> m (f b));
for foo fa = fextract (fmap foo fa);
In my libraries, [] is an instance of ExtractableFunctor, and Monad is a
subclass of FunctorApplyReturn (which has return and liftM2).
--
Ashley Yakeley, Seattle WA
More information about the Libraries
mailing list