The following code, when compiled with GHC 6.6.1 --make -O gives a stack overflow when I enter 1000000 as a command line argument:

(please don't look at the efficiency of the code, it can of course be improved a lot both in time performance and numeric precision...)

import System

leibnizPI :: Integer -> Double
leibnizPI n = sum (map leibnizTerm [0..n]) where
   leibnizTerm n = let i = fromIntegral n
               in 4 * (((-1) ** i) / (2*i+1))
main = do
 args <- getArgs
 let n = read (head args)
 print (leibnizPI n)

However, if I replace

main = print (leibnizPI 1000000)

is does not stack overflow.

Now, if I leave the original main, but replace sum in leibnizPI by

mysum xs = aux 0 xs
   where
     aux s (x:xs) = aux (s+x) xs
     aux s [] = s

then I don't get a stack overflow.

However, I do get a stack overflow when I compile it without -O, in all cases.

This puzzles me. I don't see any non-tail calls in my code...

I guess it has to do with strictness? http://www.haskell.org/haskellwiki/Performance/Strictness

Why isn't it possible to annotate strictness on the type signature in Haskell as in Clean? Is this on the TODO list?

Many thanks,
Peter


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to