I have a stupid question :) Is there something like "while" loop in GHC ?? Thanks very much :)
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. -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume On Wed, 11 Jun 2003, Filip wrote:
I have a stupid question :) Is there something like "while" loop in GHC ??
Thanks very much :) _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
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
participants (3)
-
Filip -
Hal Daume III -
Tomasz Zielonka