<div dir="ltr">When you do computations involving IO, you want to be in IO context.<div><br></div><div>For instance, let's say we want to count the amount of rows in a file, and we're going to use readFile for this</div><div><br></div><div>First, lets look at the types </div><div><br></div><div>readFile :: FilePath -> IO String</div><div><br></div><div>This is a computation with an IO result, which means we should only access it by IO actions.</div><div><br></div><div><div>computeLines :: String -> Int</div><div>computeLines = length . lines</div><div><br></div></div><div>Lines is an example computation for calculating the amount of lines in a file. It's composed of the function lines :: String -> [String] which splits a string into a list of strings, divided by line break. </div><div><br></div><div>This is the function we want to use the result of readFile on, but as it's IO, the type system won't let us. However, there's hope still</div><div><br></div><div>countLines :: FilePath -> IO Int</div><div>countLines fp = do </div><div> file <- readFile fp</div><div> return $ computeLines file </div><div><br></div><div><br></div><div>By the powers of the do notation and IO actions we can compose the functions and still live in the comfortable context of IO actions. </div><div><br></div><div>If you're not used to IO computations, this snippet will contain some concepts that probably are new to you:</div><div>* The "do" notation; it's a syntactic sugar for doing sequential computations within a type, like IO actions. </div><div>* The left pointing arrow (<-) is a type-safe way to extract a contextual value (like IO String) into a computable value (like String).</div><div>* return packages a computation (like Int) into a contextual value (like IO Int).</div><div><br></div><div>So what we do is fetching the result from readFile with <-, computes it (computeLines file) and package it from an Int to an IO Int. </div><div><br></div><div>Best regards, </div><div>Jonathan</div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-05-25 14:29 GMT+02:00 Magnus Therning <span dir="ltr"><<a href="mailto:magnus@therning.org" target="_blank">magnus@therning.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 25 May 2015 at 08:44, Dananji Liyanage <<a href="mailto:dan9131@gmail.com">dan9131@gmail.com</a>> wrote:<br>
> Hi All,<br>
><br>
> I'm writing a code, where the input is read from a text file using:<br>
> readValues = readFile "Input.txt"<br>
><br>
> Since the type of this is 'IO String', I can't use this in the consequent<br>
> functions.<br>
><br>
> For an example: I want to split this as follows within another function<br>
><br>
> extractInput url method template<br>
> | isURI url == True = getList values components<br>
> | otherwise = []<br>
> where components = splitTemplate readValues<br>
> values = getURL (splitURL url) method<br>
><br>
> This gives the following error:<br>
><br>
> Couldn't match type ‘IO String’ with ‘[Char]’<br>
> Expected type: String<br>
> Actual type: IO String<br>
><br>
> How can I solve this?<br>
<br>
</div></div>Start with reading some basic Haskell book/tutorial. That should tell<br>
you how to e.g. use 'do' notation, or `liftM`, to achieve what you<br>
want.<br>
<span class="HOEnZb"><font color="#888888"><br>
/M<br>
<br>
--<br>
Magnus Therning OpenPGP: 0xAB4DFBA4<br>
email: <a href="mailto:magnus@therning.org">magnus@therning.org</a> jabber: <a href="mailto:magnus@therning.org">magnus@therning.org</a><br>
twitter: magthe <a href="http://therning.org/magnus" target="_blank">http://therning.org/magnus</a><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>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Jonathan Skårstedt<br>Bergsgårdsgärdet 39<br>Lgh 1108<br>424 32 Göteborg<br>Mobil: 073 - 76 20 20 7<br></div>
</div>