[Haskell-cafe] Spurious program crashes

Simon Marlow simonmar at microsoft.com
Wed Nov 23 09:11:12 EST 2005


On 23 November 2005 13:29, Joel Reymont wrote:

> On Nov 23, 2005, at 1:18 PM, Simon Marlow wrote:
> 
>> After subsequent dicsussion, do you still think something strange was
>> going on here?
> 
> Yes, but in a different thread. The "Postmortem" one.
> 
>> so this basically loops until there are no messages in the channel,
>> and then exits.  Is that what you wanted, or did you want it to keep
>> reading from the channel until told to die?
> 
> I probably made a mistake someplace as I wanted to read until told to
> die but ONLY if the channel was empty. I replaced that code with
> Tomasz's elegant solution so now I read until Nothing is read from
> the channel.
> 
> logger :: Handle -> IO ()
> logger h =
>      do ss <- getChanContents parent
>         logger' ss
>         where logger' [] = return ()
>               logger' (Nothing:_) = return ()
>               logger' ((Just x):xs) =
>                   do putStrLn x
>                      hPutStrLn h x
>                      logger' xs
>                      yield

The yield is unnecessary.  Also, getChanContents is considered by some
(me included) to be poor style, because it relies on lazy I/O.  This
should work just as well:

  logger h = do
     m <- readChan parent
     case m of
       Nothing -> return ()
       Just x  -> do hPutStrLn h x; logger h

Cheers,
	Simon


More information about the Haskell-Cafe mailing list