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

Derek Elkins derek.a.elkins at gmail.com
Thu Mar 5 19:26:46 EST 2009


On Thu, 2009-03-05 at 16:12 -0800, Jonathan Cast wrote:
> 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.

Both are poorish style.

reader <- forkIO $ forever $ do (nr', line) <- readChan; when (nr /= nr') $ putStrLn hdl line



More information about the Haskell-Cafe mailing list