[Haskell-cafe] Flattening tail recursion?

Henning Thielemann iakd0 at clusterf.urz.uni-halle.de
Fri Dec 10 13:07:04 EST 2004


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



More information about the Haskell-Cafe mailing list