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. Re:  File I/O: reading from, writing to, same file in one
      function (Chadda? Fouch?)
   2. Re:  File I/O: reading from, writing to, same file in one
      function (Chadda? Fouch?)
   3. Re:  File I/O: reading from, writing to, same file in one
      function (Geoffrey Bays)
   4. Re:  File I/O: reading from, writing to, same file in one
      function (Chadda? Fouch?)
   5. Re:  File I/O: reading from, writing to, same file in one
      function (Geoffrey Bays)


----------------------------------------------------------------------

Message: 1
Date: Thu, 5 Feb 2015 22:33:11 +0100
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] File I/O: reading from, writing to,
        same file in one function
Message-ID:
        <canfjzrbdkcvhtlqcgajzudhzxtspa6dcirpjmabfnc4bs-q...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Thu, Feb 5, 2015 at 10:30 PM, Geoffrey Bays <[email protected]>
wrote:

> Francesco:
>
> Unfortunately, using withFile I get the same error as before with handles:
> hPutStr: illegal operation (handle is closed).
>
>
This error suggests you're forgetting to reopen your file in WriteMode once
withFile closed it (you *did *read the documentation of withFile ?). Trying
to write on a closed handle is an exercise in futility.

-- 
Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150205/dd7e3827/attachment-0001.html>

------------------------------

Message: 2
Date: Thu, 5 Feb 2015 22:42:38 +0100
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] File I/O: reading from, writing to,
        same file in one function
Message-ID:
        <CANfjZRbzF-7VoAu=v-VyDYzO57queBPY504duDVjK-=iwn3...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

If you're still wedded to your notion (I strongly urge you to use the
proper way with temp file and renaming, even making it a function if you
need to repeat this several times) :

readModifyWrite fileName modify = do
  contents <- readFile fileName
  evaluate (length contents) -- ensure that contents is evaluated and the
handle closed
  writeFile fileName (modify contents)

This should work (but may consume a fair bit of memory, you should probably
use Text or ByteString anyway and use the proper way instead).

-- 
Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150205/2df91962/attachment-0001.html>

------------------------------

Message: 3
Date: Thu, 5 Feb 2015 16:43:29 -0500
From: Geoffrey Bays <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] File I/O: reading from, writing to,
        same file in one function
Message-ID:
        <cad8ukx6v1phxfnfj7ebx8a3nwuvd7fzf7eab3phz2_by9uf...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Chaddai:
Thanks for your excellent explanation of why using a temp file is necessary
and a good thing to do.

The problem also with using withFile and a lambda is that in my infinite
Haskell beginnerness, I do not know how to get the
contents out of the lambda for use later for the second try of withFile in
WriteMode.

As in LYAH

   1.     withFile "girlfriend.txt" ReadMode (\handle -> do
   2.         contents <- hGetContents handle
   3.         putStr contents)

how to retrieve contents for later use? Scope of contents variable in
inside lambda only it would appear.

Can I declare an empty contents IO String before the withFile lambda
somehow?


Thanks


On Thu, Feb 5, 2015 at 4:33 PM, Chadda? Fouch? <[email protected]>
wrote:

> On Thu, Feb 5, 2015 at 10:30 PM, Geoffrey Bays <[email protected]>
> wrote:
>
>> Francesco:
>>
>> Unfortunately, using withFile I get the same error as before with
>> handles: hPutStr: illegal operation (handle is closed).
>>
>>
> This error suggests you're forgetting to reopen your file in WriteMode
> once withFile closed it (you *did *read the documentation of withFile ?).
> Trying to write on a closed handle is an exercise in futility.
>
> --
> Jeda?
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150205/82740db0/attachment-0001.html>

------------------------------

Message: 4
Date: Thu, 5 Feb 2015 22:55:08 +0100
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] File I/O: reading from, writing to,
        same file in one function
Message-ID:
        <CANfjZRbK=ydm1bkke3-8ng1ixoey07ui8qnuvi-dfcx8nd9...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Thu, Feb 5, 2015 at 10:43 PM, Geoffrey Bays <[email protected]>
wrote:
>
> The problem also with using withFile and a lambda is that in my infinite
> Haskell beginnerness, I do not know how to get the
> contents out of the lambda for use later for the second try of withFile in
> WriteMode.
>
> As in LYAH
>
>    1.     withFile "girlfriend.txt" ReadMode (\handle -> do
>    2.         contents <- hGetContents handle
>    3.         putStr contents)
>
> how to retrieve contents for later use? Scope of contents variable in
> inside lambda only it would appear.
>

Well withFile type is "FilePath -> Mode -> (Handle -> IO a) -> IO a", note
that "IO a", this "a" is a type variable that can be any type, this means
that the action you do in your lambda may return any type, and the type
returned by the whole withFile action is also "a", since this can be
anything, withFile can't invent it, so this must be the same thing that the
action in your lambda returned. Thus you can return the content you wished
for :

  contents <- withFile fileName ReadMode $ \h -> do
    contentsInside <- hGetContents handle
    evaluate (length contentsInside) -- still the same, you have to
evaluate the whole contents now since withFile will close the handle as
soon as you exit your lambda, use a strict variant of hGetContents to avoid
this line
    return contentsInside

Note this is convoluted, using a strict variant of hGetContents would
allows you to just go : contents <- withFile fileName ReadMode $ \h ->
hGetContents h
Text for instance provide such a strict variant in Data.Text.IO (but you
should then just use its strict variant of readFile which is exactly
equivalent to what I just wrote).

-- 
Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150205/7b87556b/attachment-0001.html>

------------------------------

Message: 5
Date: Thu, 5 Feb 2015 17:03:57 -0500
From: Geoffrey Bays <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] File I/O: reading from, writing to,
        same file in one function
Message-ID:
        <CAD8ukx68-tvBY8z2_nG7Yxyr8iincUO5dZJNB9=yspwdcpg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Thanks, Chaddai. It seems quite obvious once you explain it. And I now know
that ghci :t is my friend indeed.

Geoffrey

On Thu, Feb 5, 2015 at 4:55 PM, Chadda? Fouch? <[email protected]>
wrote:

> On Thu, Feb 5, 2015 at 10:43 PM, Geoffrey Bays <[email protected]>
> wrote:
>>
>> The problem also with using withFile and a lambda is that in my infinite
>> Haskell beginnerness, I do not know how to get the
>> contents out of the lambda for use later for the second try of withFile
>> in WriteMode.
>>
>> As in LYAH
>>
>>    1.     withFile "girlfriend.txt" ReadMode (\handle -> do
>>    2.         contents <- hGetContents handle
>>    3.         putStr contents)
>>
>> how to retrieve contents for later use? Scope of contents variable in
>> inside lambda only it would appear.
>>
>
> Well withFile type is "FilePath -> Mode -> (Handle -> IO a) -> IO a", note
> that "IO a", this "a" is a type variable that can be any type, this means
> that the action you do in your lambda may return any type, and the type
> returned by the whole withFile action is also "a", since this can be
> anything, withFile can't invent it, so this must be the same thing that the
> action in your lambda returned. Thus you can return the content you wished
> for :
>
>   contents <- withFile fileName ReadMode $ \h -> do
>     contentsInside <- hGetContents handle
>     evaluate (length contentsInside) -- still the same, you have to
> evaluate the whole contents now since withFile will close the handle as
> soon as you exit your lambda, use a strict variant of hGetContents to avoid
> this line
>     return contentsInside
>
> Note this is convoluted, using a strict variant of hGetContents would
> allows you to just go : contents <- withFile fileName ReadMode $ \h ->
> hGetContents h
> Text for instance provide such a strict variant in Data.Text.IO (but you
> should then just use its strict variant of readFile which is exactly
> equivalent to what I just wrote).
>
> --
> Jeda?
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150205/6cc83c74/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 80, Issue 7
****************************************

Reply via email to