[Haskell-beginners] Using IO values for computations

Dananji Liyanage dan9131 at gmail.com
Tue May 26 03:29:01 UTC 2015


Thank you for the nice explanation!

I understood it for some extent, and will read further on I/O.

On Mon, May 25, 2015 at 9:53 PM, Kostiantyn Rybnikov <k-bx at k-bx.com> wrote:

> Dananji,
>
> 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.
>
> main :: IO ()
> main = do
>     s <- readFile "somefile.txt"
>     putStrLn (show (doSplit s))
>
> 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:
>
> main :: IO ()
> main =
>     readFile "somefile.txt" >>= (\s ->
>     putStrLn (show (doSplit s)))
>
> I highly recommend reading "Learn You A Haskell" book
> http://learnyouahaskell.com/ , which explained these concepts really well
> to me.
>
> On Mon, May 25, 2015 at 9:44 AM, Dananji Liyanage <dan9131 at gmail.com>
> wrote:
>
>> Hi All,
>>
>> I'm writing a code, where the input is read from a text file using:
>> readValues = readFile "Input.txt"
>>
>> Since the type of this is 'IO String', I can't use this in the
>> consequent functions.
>>
>> For an example: I want to split this as follows within another function
>>
>> extractInput url method template
>>   | isURI url == True = getList values components
>>   | otherwise = []
>>   where components = splitTemplate readValues
>>         values = getURL (splitURL url) method
>>
>> This gives the following error:
>>
>>  Couldn't match type ‘IO String’ with ‘[Char]’
>>     Expected type: String
>>       Actual type: IO String
>>
>> How can I solve this?
>>
>> Thanks in advance!
>>
>> --
>> Regards,
>> Dananji Liyanage
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>


-- 
Regards,
Dananji Liyanage
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150526/b53426a1/attachment-0001.html>


More information about the Beginners mailing list