<div dir="ltr"><div><div><div><div><div><div><div><div>Dananji,<br><br></div>Haskell explicitly separates "pure" from "impure", so "readFile <file>" returns not a string, but rather an action, which tells haskell to read a string. In order to "use" a result of some IO action as a pure value, you have several ways, most popula of which is a do-notation.<br><br></div>main :: IO ()<br></div>main = do<br></div> s <- readFile "somefile.txt"<br></div> putStrLn (show (doSplit s))<br><br></div>In the code above, variable "s" is "pure", it has type String and you can use it as you want. Do-notation is essentially a sugar to callback mechanism, where in order to use a value of some IO-computation you write a function-callback that will be called with a pure value of computation result. This code is the same as previous one, but without do-notation:<br><br></div>main :: IO ()<br>main =<br></div><div> readFile "somefile.txt" >>= (\s -><br></div><div> putStrLn (show (doSplit s)))<br><br></div><div>I highly recommend reading "Learn You A Haskell" book <a href="http://learnyouahaskell.com/">http://learnyouahaskell.com/</a> , which explained these concepts really well to me.<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 25, 2015 at 9:44 AM, Dananji Liyanage <span dir="ltr"><<a href="mailto:dan9131@gmail.com" target="_blank">dan9131@gmail.com</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"><div class="gmail_default" style="font-family:georgia,serif;font-size:small">Hi All,</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><br></div><div class="gmail_default" style="font-size:small"><span style="font-family:georgia,serif">I'm writing a code, where the input is read from a text file using: </span></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace">readValues = readFile "Input.txt"</font></div><div class="gmail_default" style="font-size:small"><font face="monospace, monospace"><br></font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif">Since the type of this is '</font><font face="monospace, monospace">IO String'</font><font face="georgia, serif">, I can't use this in the consequent functions.</font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif"><br></font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif">For an example: I want to split this as follows within another function</font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif"><br></font></div><div class="gmail_default" style="font-size:small"><div class="gmail_default"><font face="monospace, monospace">extractInput url method template</font></div><div class="gmail_default"><font face="monospace, monospace"> | isURI url == True = getList values components</font></div><div class="gmail_default"><font face="monospace, monospace"> | otherwise = []</font></div><div class="gmail_default"><font face="monospace, monospace"> where components = splitTemplate readValues</font></div><div class="gmail_default"><font face="monospace, monospace"> values = getURL (splitURL url) method</font></div><div class="gmail_default"><font face="monospace, monospace"><br></font></div><div class="gmail_default"><font face="georgia, serif">This gives the following error:</font></div><div class="gmail_default"><font face="monospace, monospace"><br></font></div><div class="gmail_default"><font face="monospace, monospace" color="#ff0000"><div class="gmail_default"> Couldn't match type ‘IO String’ with ‘[Char]’</div><div class="gmail_default"> Expected type: String</div><div class="gmail_default"> Actual type: IO String</div></font></div></div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif">How can I solve this?</font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif"><br></font></div><div class="gmail_default" style="font-size:small"><font face="georgia, serif">Thanks in advance! </font></div><span class="HOEnZb"><font color="#888888"><div><br></div>-- <br><div><div dir="ltr"><div><div><div><font face="georgia,serif">Regards, <br>Dananji Liyanage</font></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>