[Haskell-cafe] Puzzled

Peter Verswyvelen bf3 at telenet.be
Fri Oct 5 15:22:21 EDT 2007

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

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

I guess it has to do with 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,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20071005/2aa68e3d/attachment-0001.htm

More information about the Haskell-Cafe mailing list