[Haskell-cafe] Flattening tail recursion?

GoldPython goldpython at gmail.com
Fri Dec 10 13:55:03 EST 2004


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
<iakd0 at clusterf.urz.uni-halle.de> wrote:
> 
> 
> 
> 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