[Haskell-cafe] Flattening tail recursion?

Bayley, Alistair Alistair_Bayley at ldn.invesco.com
Fri Dec 10 10:29:00 EST 2004


A common technique to make a function tail-recursive is to introduce an
accumulating parameter, although in the 5 minutes I've spent looking on the
wiki, there's no specific page for this. Perhaps add a link from the
Efficiency section on  http://haskell.org/hawiki/CommonHaskellIdioms  ?

> -----Original Message-----
> From: GoldPython [mailto:goldpython at gmail.com] 
> Sent: 10 December 2004 15:07
> To: haskell-cafe at haskell.org
> Subject: [Haskell-cafe] Flattening tail recursion?
> I'm missing something, a functional idiom, whatever. I know there's
> "length" to count the elements in a list and it works fine, but the
> function below stack overflows on a large list.
>    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? Is
> it because the call is embedded in an expression?
> And additionally, how can I predict when the flattening will occur?
> Thanks,
> Will
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

Confidentiality Note: The information contained in this   message, and any
attachments, may contain confidential   and/or privileged material. It is
intended solely for the   person(s) or entity to which it is addressed. Any
review,   retransmission, dissemination, or taking of any action in
reliance upon this information by persons or entities other   than the
intended recipient(s) is prohibited. If you received  this in error, please
contact the sender and delete the   material from any computer.

More information about the Haskell-Cafe mailing list