[Haskell-cafe] Tail-call optimization

ajb at spamcop.net ajb at spamcop.net
Sun Dec 11 18:50:01 EST 2005


G'day.

Quoting Joel Reymont <joelr1 at gmail.com>:

> writeLoop :: (Event a -> IO ()) -> Handle -> (SSL, BIO, BIO) -> IO ()
> writeLoop post h ssl =
>      do handle (\e -> post $ NetworkError e) $
>                do cmd <- read h ssl
>                   post $! Cmd $! cmd
>         writeLoop post h ssl

Good, but even better is this:

writeLoop :: (Event a -> IO ()) -> Handle -> (SSL, BIO, BIO) -> IO ()
writeLoop post h ssl
  = loop
  where
    loop = do handle (\e -> post $ NetworkError e) $
                do cmd <- read h ssl
                   post $! Cmd $! cmd
              loop

Avoiding parameter passing can make your code a lot easier to read.

Cheers,
Andrew Bromage


More information about the Haskell-Cafe mailing list