[Haskell-cafe] do you have to use fix with forkio?

Jonathan Cast jonathanccast at fastmail.fm
Thu Mar 5 19:12:55 EST 2009


On Thu, 2009-03-05 at 15:36 -0800, Daryoush Mehrtash wrote:
> In this chat server implementation
> http://www.haskell.org/haskellwiki/Implement_a_chat_server
> 
> forkIO is used with fix as in:
> 
> reader <- forkIO $ fix $ \loop -> do
> 
>         (nr', line) <- readChan chan'
>         when (nr /= nr') $ hPutStrLn hdl line
> 
>         loop
> 
> Do you have to use fix?  Or is there a way to write this with a "let"?

You can certainly use let:

  reader <- forkIO $ let loop = do
      (nr', line) <- readChan chan'
      when (nr /= nr') $ hPutStrLn hdl line
      loop
    in loop

But the version with fix is clearer (at least to people who have fix in
their vocabulary) and arguably better style.

jcc




More information about the Haskell-Cafe mailing list