# [Haskell-cafe] How to implement nested loops with tail recursion?

sdiyazg at sjtu.edu.cn sdiyazg at sjtu.edu.cn
Wed Sep 19 20:00:44 CEST 2012

```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

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

,

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

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

all overflows after correctly printing the first number

----- 原始邮件 -----

On Wed, Sep 19, 2012 at 7:24 PM,  <sdiyazg at sjtu.edu.cn> wrote:
> 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

```