
So how do I force IO actions whose results are discarded (including IO ()) to be strict?
main = do
s<-newIORef (1::Int)
let
f :: Int -> Int -> IO Int
f 0 !acc = return acc -- note strict accumulator
f n !acc = do
v <- modifyIORef s (+2) >>readIORef s -- reading immediately after writing
f (n-1) (v+acc)
f 1000000 100 >>= print
readIORef s>>=print
runs OK, while
main = do
s<-newIORef (1::Int)
let
f :: Int -> Int -> IO Int
f 0 !acc = return acc -- note strict accumulator
f n !acc = do
v <- modifyIORef s (+2) >>return 1
f (n-1) (v+acc)
f 1000000 100 >>= print
readIORef s>>=print
,
main = do
s<-newIORef (1::Int)
let
f :: Int -> Int -> IO Int
f 0 !acc = return acc -- note strict accumulator
f n !acc = do
v <- modifyIORef s (+2) >>readIORef s>>return 1
f (n-1) (v+acc)
f 1000000 100 >>= print
readIORef s>>=print
and
main = do
s<-newIORef (1::Int)
let
f :: Int -> Int -> IO Int
f 0 !acc = return acc -- note strict accumulator
f n !acc = do
v <- (>>return 1) $! (modifyIORef s (+2) >>readIORef s)
f (n-1) (v+acc)
f 1000000 100 >>= print
readIORef s>>=print
all overflows after correctly printing the first number
----- 原始邮件 -----
发件人: "Johan Tibell"
main = do let f 0 acc = return acc f n acc = do v <- return 1 f (n-1) (v+acc) f 1000000 100 >>= print
Try this main = do let f :: Int -> Int -> IO Int f 0 !acc = return acc -- note strict accumulator f n acc = do v <- return 1 f (n-1) (v+acc) f 1000000 100 >>= print