[Haskell] Monadic Loops

Arjan van IJzendoorn afie at cs.uu.nl
Thu Jun 17 07:59:01 EDT 2004


Vivian uses this while function:

while test body = do
                  (cond,res) <- body
                  if (test cond) then do rs <- while test body
                                      return (res:rs)
                  else return [res]

> However, when I run the program, the interpreter falls over after a few
thousand iterations, running out of space.

Maybe making it tail-recursive helps. Let me think...... untested code

while test body =
        res <- funnyWhile test body []
        return (reverse res)

funnyWhile test body revResult =
        (cond, res) <- body
        if test cond then
            while test body (res:revResult)
            return revResult

Greetings, Arjan

More information about the Haskell mailing list