main :: IO ()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:putStrLn (show (doSplit s))s <- readFile "somefile.txt"main = domain :: IO ()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 =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@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 functionextractInput url method template| isURI url == True = getList values components| otherwise = []where components = splitTemplate readValuesvalues = getURL (splitURL url) methodThis gives the following error:Couldn't match type ‘IO String’ with ‘[Char]’Expected type: StringActual type: IO StringHow can I solve this?Thanks in advance!--Regards,
Dananji Liyanage
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners