
In article <878y60v8hk.fsf@peti.cryp.to>, Peter Simons
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