[Haskell-cafe] How to read a file and return a String?

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Wed Sep 4 16:36:52 CEST 2013


On Wed, Sep 04, 2013 at 10:21:37PM +0800, yi lu wrote:
> I want to read a text file, and store it in a *String*. But readFile will
> get *IO String*. I search with google and they tell me it is not
> necessarily to do so. Can you explain to me why is this? Furthermore, How
> to read a file and store it in a String?

You need to lift 'lines' into the IO functor, rather than trying to "remove"
the String from IO (which doesn't make sense).  Try

    fmap lines (readFile "filename")

Tom








More information about the Haskell-Cafe mailing list