Hi,

thanks for your investigation. I've only some basic knowledge about a narrowing
machine, so I don't understand the words 'thunk' and these. Is there a paper or
documentation available somewhere?

> If the definition of seq is inlined, then we get the (much better):
> 
>                 case z of z' -> foldl' f z' xs
> 

Did I understand the strictness of the case statement right:
case z of z' forces that z' (and z) will be in head normalform?

> I checked this with both ghc-3.02 and ghc-4.00 with and without -O,
> and except for ghc-4.00 without -O they all require a constant 200
> bytes or so of heap.

I got other results. With -O and ghc-3.02 (pl0) the program below (simplified
version of the 5-line example) needs linear heap space, without -O it needs
constant heap space (about 1KB).

So I'm confused!

That's my whole test program:


module Main where
import Numeric

-------------------------------------------------------------------------------

seq1 :: Int -> [Int]
seq1 x = x:seq1 x

-------------------------------------------------------------------------------

foldl'           :: Eval a => (a -> b -> a) -> a -> [b] -> a
foldl' f a []     = a
foldl' f a (x:xs) = strict (foldl' f) (f a x) xs

numberOfOne :: Int -> Int
numberOfOne n = foldl' (\x y -> 0) 0 (take n (seq1 1))

-------------------------------------------------------------------------------

readNum :: Integral a => String -> a
readNum = fst . head . readDec

main = do
    putStr "n = "
    input <- getLine
    n <- return (readNum input)
    output <- return (numberOfOne n)
    putStrLn (show output)

Reply via email to