
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
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners