
10 Dec
2004
10 Dec
'04
1:55 p.m.
I did this:
countLines ls = foldl (\x y -> x + 1) 0 ls
Still overflows.
On Fri, 10 Dec 2004 19:07:04 +0100 (MEZ), Henning Thielemann
On Fri, 10 Dec 2004, Robert Dockins wrote:
countLines [] = 0 countLines (_:ls) = 1 + countLines ls
I would have thought that this was tail recursive and would be flattened into iteration by the compiler. Can anyone explain why?
countlines = aux 0 where aux x [] = x aux x (_:ls) | x `seq` False = undefined | otherwise = aux (x+1) ls
The `seq` causes the x to be evaluated, so it should run in constant space.
Is it also possible to do that with 'foldl'?
Why is Prelude.length not defined this way (according to the Haskell98 report)?