<div dir="ltr"><div>Thanks. I played around some more with return and came up with this which compiles fine:<br><br>readData :: String -> IO [String]<br>readData classNameP = do<br> let fileName = classNameP ++ ".txt"<br> contents <- readFile fileName<br> return $ lines contents<br><br></div>Geoffrey<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 19, 2015 at 12:25 PM, Sumit Sahrawat, Maths & Computing, IIT (BHU) <span dir="ltr"><<a href="mailto:sumit.sahrawat.apm13@iitbhu.ac.in" target="_blank">sumit.sahrawat.apm13@iitbhu.ac.in</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">The type should be IO [String]. You do IO, and it results in a list of Strings.<div>Also, take a look at the following types:</div><div><br></div><div><font face="monospace, monospace"> contents :: String</font></div><div><font face="monospace, monospace"> lines :: String -> [String]</font></div><div><br></div><div>Which means that,</div><div><br></div><div>lines contents :: [String]</div><div><br></div><div>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:</div><div><br></div><div><font face="monospace, monospace"> return :: Monad m => a -> m a</font></div><div><br></div><div>Not specific to IO, but all monads.</div><div>Hope this helps.</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On 19 February 2015 at 22:47, Geoffrey Bays <span dir="ltr"><<a href="mailto:charioteer7@gmail.com" target="_blank">charioteer7@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><div><div><div><div><div><div>Haskellers:<br><br></div>I want to write a function that takes a class name and reads from a file and then returns the<br></div>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:<br><br>readData :: String -> IO String -- what type here??<br>readData classNameP = do<br> let fileName = classNameP ++ ".txt"<br> contents <- readFile fileName<br> lines contents<br><br></div>Not to complain, but this would not be difficult in any other language I have tried, so I could use<br></div>some explanation. I suspect it has to do with understanding the IO Monad.<br><br></div>Many Thanks<span><font color="#888888"><br><br></font></span></div><span><font color="#888888">Geoffrey<br></font></span></div>
<br></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</font></span></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>