[Haskell-cafe] Deadlock in real number multiplication (Was: Where's the problem ?)

Philip Armstrong phil at kantaka.co.uk
Thu Jul 5 05:32:19 EDT 2007


On Thu, Jul 05, 2007 at 10:49:17AM +0400, Miguel wrote:
>R> Another interesting thing I've discovered is:
>
>Prelude>> length [1..1000000]
>R> 1000000
>Prelude>> Data.List.genericLength [1..1000000]
>R> *** Exception: stack overflow
>
>R> Maybe there is something wrong with Integer ? 
>
>No, there is something wrong with genericLength:
>
>Prelude> Data.List.foldl (+) 0 $ map (const 1) [1..1000000] :: Int
>*** Exception: stack overflow
>Prelude> Data.List.foldl' (+) 0 $ map (const 1) [1..1000000] :: Integer
>1000000

 From ghc/libraries/base/Data/List.hs:

genericLength           :: (Num i) => [b] -> i
genericLength []        =  0
genericLength (_:l)     =  1 + genericLength l

So genericLength is lazily building up unevaluated (+) expressions and
running out of stack space.

Is there a good reason for genericLength to be lazy?

Phil

-- 
http://www.kantaka.co.uk/ .oOo. public key: http://www.kantaka.co.uk/gpg.txt


More information about the Haskell-Cafe mailing list