[Haskell-beginners] IO String and String using readFile

Theodore Lief Gannon tanuki at gmail.com
Wed Mar 27 03:49:39 UTC 2019


I'm guessing in the .hs file, you have this in a do block? Something like:

foo = do
    contents <- readFile
    words contents

If so, the problem is the return type of `words`. Each line of the do block
has to have an IO value, and words is a pure function. To lift its result
into the monad, use the aptly-named `pure` function:

foo = do
    contents <- readFile
    pure $ words contents


On Tue, Mar 26, 2019, 8:35 PM Yugesh Kothari <kothariyugesh at gmail.com>
wrote:

> Hi,
>
> I am trying to read data from a file.
> When I do-
> ghci> contents <- readFile "file.txt"
> ghci> words contents
>
> Then everything works.
>
> The same thing when done in a .hs file gives an IO String vs [String]
> error.
>
> Why is that so?
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20190326/2a38966f/attachment.html>


More information about the Beginners mailing list