[Haskell] Writer monad

Emil Axelsson emax at cs.chalmers.se
Tue Feb 15 11:45:26 EST 2005


Hello,

I have a huge space leak in a program due to laziness in the writer monad. Now
when I'm trying to examine the behaviour of writer I get a bit puzzled by the
following program:


    foo :: MonadWriter [Int] m => Int -> m ()
    foo n = do tell [n]
               foo $ n+1

    test  = (snd $ runWriter $ foo 0) !! 3

    testT = (snd $ runIdentity $ runWriterT $ foo 0) !! 3


I would expect both test and testT to terminate with the value 3, due to the
laziness that caused me problems. But here is the actual run results:


    Ok, modules loaded: Main.
    *Main> test
    3
    *Main> testT
    *** Exception: stack overflow
    *Main>


Could someone please explain this to me?

/ Emil



More information about the Haskell mailing list