[Haskell-cafe] How to implement nested loops with tail recursion?
Johan Tibell
johan.tibell at gmail.com
Wed Sep 19 19:28:47 CEST 2012
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
More information about the Haskell-Cafe
mailing list