[Haskell-cafe] Getting WriterT log lazily

Magnus Therning magnus at therning.org
Sun May 3 17:17:11 EDT 2009

I've been playing around with (WriterT [Int] IO), trying to get the log
out and map `print` over it... and do it lazily.  However, I'm not
really happy with what I have so far, since I've had to resort to
`unsafePerformIO`.  Any hints are welcome.

What I have so far is:

   foo = let
           _tell i = do
               a <- return $ unsafePerformIO $ sleep 1
               tell [a + 1 `seq` i]
       in do
           mapM_ _tell [1..10]

   main = do
       (_, ~res) <- runWriterT foo
       mapM_ print res

Without the `seq` the call to sleep will simply be skipped (is there an
easier way to force evaluation there?).  Without `unsafePerformIO` all
the sleeping is done up front, and all numbers are print at once at the

The goal is of course to use code along the same shape to do something 
more useful, and then `unsafePerformIO` will really be unsafe...


Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090503/df976534/signature.bin

More information about the Haskell-Cafe mailing list