[Haskell-beginners] IO loops with tail call

Brent Yorgey byorgey at seas.upenn.edu
Sat Nov 6 15:26:49 EDT 2010

On Sun, Nov 07, 2010 at 01:54:26AM +0900, Yasuyuki Ogawa wrote:
> Hello!
> I wrote a simple echo program which end up with input "q":
> echo = do
>   str <- getLine
>   if (str == "q")
>     then return ()
>     else putStrLn str >> echo

This is a tail call.  Yes, if expressions return a value, but here
nothing more is done with that value so the memory used by the current
call to echo can be garbage collected once the recursive call is made.

However, the real answer is: don't worry about it!  Because of
Haskell's lazy evaluation, the notion of "tail call" is not very
important. And even if it were, you still shouldn't worry about it:
just write your Haskell programs in the most natural, beautiful style
you can think of.  Maybe at some point down the road you will have
to start worrying about optimization.  But you should put it off as
long as possible.


More information about the Beginners mailing list