[Haskell-beginners] function to read file and return list of contents not compiling

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Thu Feb 19 17:29:00 UTC 2015


Thus,

    return (lines contents)

will work.

On 19 February 2015 at 22:55, Sumit Sahrawat, Maths & Computing, IIT (BHU) <
sumit.sahrawat.apm13 at iitbhu.ac.in> wrote:

> The type should be IO [String]. You do IO, and it results in a list of
> Strings.
> Also, take a look at the following types:
>
>     contents :: String
>     lines :: String -> [String]
>
> Which means that,
>
> lines contents :: [String]
>
> But because you're dealing with a monad (IO in this case), this will not
> typecheck. You can convert a pure value to a monadic value using the return
> function:
>
>     return :: Monad m => a -> m a
>
> Not specific to IO, but all monads.
> Hope this helps.
>
> On 19 February 2015 at 22:47, Geoffrey Bays <charioteer7 at gmail.com> wrote:
>
>> Haskellers:
>>
>> I want to write a function that takes a class name and reads from a file
>> and then returns the
>> list of String of the file lines. I have experimented with various return
>> types, but GHC does not like any of them. Here is my function:
>>
>> readData :: String -> IO String   -- what type here??
>> readData classNameP = do
>>     let fileName = classNameP ++ ".txt"
>>     contents <- readFile fileName
>>     lines contents
>>
>> Not to complain, but this would not be difficult in any other language I
>> have tried, so I could use
>> some explanation. I suspect it has to do with understanding the IO Monad.
>>
>> Many Thanks
>>
>> Geoffrey
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
>
> --
> Regards
>
> Sumit Sahrawat
>



-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150219/4808f0d9/attachment.html>


More information about the Beginners mailing list