Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Buggy behavior of "withFile" (Zoran Plesiv?ak)
2. Re: Buggy behavior of "withFile" (Christopher Reichert)
3. Re: Buggy behavior of "withFile" (Mike Meyer)
----------------------------------------------------------------------
Message: 1
Date: Tue, 9 Dec 2014 21:21:52 +0100
From: Zoran Plesiv?ak <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Buggy behavior of "withFile"
Message-ID:
<caoskn4uyfspw-nnvxowhhhqpujtg0eu+wrw2fuv5la3deti...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
I've encountered unexplainable of "withFile" function. Consider
"example.hs" program:
import System.IO
main = do
cnts <- withFile "example.hs" ReadMode $ (\h -> do
res <- hGetContents h
--putStr res
return res)
putStr cnts
When commented-out line is like that, program doesn't write out
anything to the STDOUT.
If "--" (commend characters) are removed, program writes out contents
of "example.hs" two times.
Is this expected behavior? I asked on #haskell (freenode) and one
fellow there found it equally confusing...
-------------------------------------------
GHC version: The Glorious Glasgow Haskell Compilation System, version 7.6.3
OS: Linux
Thanks,
Zoran Plesiv?ak
------------------------------
Message: 2
Date: Tue, 09 Dec 2014 14:34:11 -0600
From: Christopher Reichert <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Buggy behavior of "withFile"
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On Tue, Dec 09 2014, Zoran Plesiv?ak <[email protected]> wrote:
> I've encountered unexplainable of "withFile" function. Consider
> "example.hs" program:
>
> import System.IO
>
> main = do
> cnts <- withFile "example.hs" ReadMode $ (\h -> do
> res <- hGetContents h
> --putStr res
> return res)
> putStr cnts
>
> When commented-out line is like that, program doesn't write out
> anything to the STDOUT.
> If "--" (commend characters) are removed, program writes out contents
> of "example.hs" two times.
>
> Is this expected behavior? I asked on #haskell (freenode) and one
> fellow there found it equally confusing...
>
This is an example of where Lazy IO can get tricky. withFile is closing
the handle before you are able to evaluate the contents. When you use
putStr in the lambda function, `res` is evaluated (and thus evaluated
when it's returned). When it's commented, res is not evaluated before
you close the file.
See the documentation:
http://hackage.haskell.org/package/base-4.7.0.1/docs/System-IO.html#v%3awithFile
Specifically note: "The handle will be closed on exit from withFile"
Google around for "haskell lazy io withFile" and you might find more
detailed explanations.
--
Christopher Reichert
irc: creichert
gpg: C81D 18C8 862A 3618 1376 FFA5 6BFC A992 9955 929B
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141209/fb545600/attachment-0001.sig>
------------------------------
Message: 3
Date: Tue, 9 Dec 2014 14:36:50 -0600
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Buggy behavior of "withFile"
Message-ID:
<CAD=7U2B39cexZsYvFTNjDcYW-RBkDT3WY=XxO=qQ2na=uvf...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Tue, Dec 9, 2014 at 2:21 PM, Zoran Plesiv?ak <[email protected]> wrote:
> I've encountered unexplainable of "withFile" function. Consider
> "example.hs" program:
>
> import System.IO
>
> main = do
> cnts <- withFile "example.hs" ReadMode $ (\h -> do
> res <- hGetContents h
> --putStr res
> return res)
> putStr cnts
>
> When commented-out line is like that, program doesn't write out
> anything to the STDOUT.
> If "--" (commend characters) are removed, program writes out contents
> of "example.hs" two times.
>
I'm not an expert, but I believe you're problem is lazy IO. hGetContents
doesn't actually read the file until something consumes it. In the version
with the first putStr commented out, that doesn't happen until you do the
putStr outside the withFile - by which time the file handle has been
closed, so nothing ever gets read.
With the putStr inside the withFile, that consumes the contents before the
file is closed, so it gets read and then returned, where the second putStr
outputs it a second time.
If I'm right, you need to force the evaluation of res before the return,
with something like "res seq return res" instead of just "return res". Or
use the BangPatterns extension and then write "!res <- hGetContents h"
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141209/834386bc/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 78, Issue 5
****************************************