Thanks Antoine!<br><br>I was suspecting it should be the lazyness of hGetContents yesterday, before going to sleep. <br><br>You just confirmed that for me!<br><br><div class="gmail_quote">On Tue, Jan 20, 2009 at 01:44, Antoine Latter <span dir="ltr"><<a href="mailto:aslatter@gmail.com">aslatter@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">On Mon, Jan 19, 2009 at 7:30 PM, Rafael Gustavo da Cunha Pereira Pinto<br>
<<a href="mailto:RafaelGCPP.Linux@gmail.com">RafaelGCPP.Linux@gmail.com</a>> wrote:<br>
> Could someone explain why:<br>
><br>
> main= do<br>
> h<-openFile "test.cir" ReadMode<br>
> c<-hGetContents h<br>
> print c<br>
><br>
>> runhaskell test1.hs<br>
>> "* Teste\n\nR1 1 0 10\nC1 1 0 10uF\nI1 1 0 1mA\n\n.DC \n.PRINT\n"<br>
><br>
> works and<br>
><br>
><br>
> main= (withFile "test.cir" ReadMode hGetContents) >>= print<br>
><br>
>> runhaskell test1.hs<br>
>> ""<br>
><br>
> don't?<br>
<br>
</div>'hGetContents' is a lazy-IO function, which means doesn't really start<br>
reading from the handle until another function tries to consume its<br>
output.<br>
<br>
The problem is that 'print' - the consumer - is outside of the<br>
'withFile' argument, and 'withFile' guarantees that the file is closed<br>
when it finishes execution.<br>
<br>
So by the time 'hGetContents' tries to do its thing, the file handle is closed.<br>
<br>
This snippet:<br>
<br>
> main = withFile "test.cir" ReadMode $ \h -> hGetContents h >>= print<br>
<br>
puts the call to 'print' inside the argument to 'withFile', so it<br>
should work as expected.<br>
<font color="#888888"><br>
-Antoine<br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Rafael Gustavo da Cunha Pereira Pinto<br>Electronic Engineer, MSc.<br>