Input Processing Loops in Haskell

Mark Conway Wirt mark@ArsConcero.org
Tue, 13 Nov 2001 14:32:08 -0500


On Tue, Nov 13, 2001 at 05:59:19PM +0000, Keith Wansbrough wrote:
> 
> It's worth adding that Mark's initial code,
> 
>   f a 
>    | a == GET_OUT   = a
>    | otherwise      = f ( g a)
> 
> will also not grow the stack, on any decent Haskell implementation.  Tail recursion is always implemented properly here - tail recursion simply means that the result of the function is the result of the recursive call to the function, i.e., no more computation need be done once the result of the recursive call is known.
> 
> http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?tail+recursion
> 

Yep, I've verified that the stack stays manageable.  Thanks for the pointer
to tail recursion -- it's nice to know *why* something works ;-)

> Of course, laziness may well bite you and you may run out of *heap*, but that's another story for another evening!
> 

Heap usage *is* something I've come up against before.  Forcing strictness has
done wonders, and ghc's profiling capabilities are invaluable for figuring
out where the leaks may be happening (at least for someone who hasn't
developed the intuition to see those things intuitively.