[Haskell-cafe] Getting WriterT log lazily
Ryan Ingram
ryani.spam at gmail.com
Sun May 3 17:52:32 EDT 2009
How about this:
> type ActionLog v = Writer [IO v]
> myTell :: v -> ActionLog v ()
> myTell a = tell [sleep 1 >> return a]
> foo :: ActionLog Int ()
> foo = mapM_ myTell [1..10]
> main = sequence_ results where
> (_, vals) = runWriter foo
> results = map (>>= print) vals
-- ryan
On Sun, May 3, 2009 at 2:17 PM, Magnus Therning <magnus at therning.org> wrote:
> 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
> end.
>
> The goal is of course to use code along the same shape to do something more
> useful, and then `unsafePerformIO` will really be unsafe...
>
> /M
>
> --
> Magnus Therning (OpenPGP: 0xAB4DFBA4)
> magnus@therning.org Jabber: magnus@therning.org
> http://therning.org/magnus identi.ca|twitter: magthe
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
More information about the Haskell-Cafe
mailing list