LOOPS

Tomasz Zielonka t.zielonka@students.mimuw.edu.pl
Wed, 11 Jun 2003 19:02:58 +0200


On Wed, Jun 11, 2003 at 09:39:24AM -0700, Hal Daume III wrote:
> It depends how much you mean "like" :).  Usually whatever you would solve
> using a while loop in an imperative language is solved with recursion in
> Haskell.
> 
> The problem with directly having 'while' is that such a statement
> depends necessarily on mutable variables (otherwise, if the condition is
> 'true' at the start, it will never be 'false').  Moreover, there isn't
> usually a return value associated with a while statement.

How about this function?

  whileM :: Monad m => m Bool -> m a -> m ()
  whileM cond body = cond >>= (`when` (body >> whileM cond body))

Its semantics is close to that of C's while when used within IO monad. 
Personally I've never used such a function, there was always some better
solution.

Regards,
Tom

-- 
.signature: Too many levels of symbolic links