Doing IO in foldr repeats lines?
Sat, 20 Jan 2001 22:25:18 +0000
On Sat, Jan 20, 2001 at 05:08:48PM -0500, Scott Turner wrote:
> At 21:17 2001-01-20 +0000, Ian Lynagh wrote:
> > main = do _ <- foldl foo (return 14) ["qq\n", "ww\n", "ee\n"]
> > putStr ""
> > foo :: IO Int -> String -> IO Int
> > foo io_l s = do l <- io_l
> > () <- putStr s
> > io_l
> It repeats lines
> >and I really don't understand why. Is the code re-evaluated every time
> >foldl is expanded or something?
> Since the result type of foo is IO Int, it is building an IO action, in
> other words it is calculating a sequence of effects. The combined effects
> are performed by main.
Thanks, a private mail showing the evaluation highlighted my stupidity
I now see what's going on and a return l instead of io_l as the last
line is what I wanted. The mixture of io_l and return bar also explains
why I was seeing really random looking output unlike the nice pattern
of the more minimal case :-)