io monad question

Wojciech Moczydlowski, Jr khaliff@astercity.net
Sun, 13 May 2001 11:07:54 +0200 (CEST)


On Sun, 13 May 2001, luc wrote:

> i designed some basic functions, , mainly String -> [String], or similar with types, plus some more complex datatypes.
> 
> Up to now; i was testing them on a basic database I was creating in the core main, "by hand".
> Then i added a basic parser with happy; and bingo; my test database is now loaded from a file... as IO String..; huh!
> 
> after a second though; this makes sense, except that i have to change all the type of my functions; now IO String..
> 
> plus a few that just dont compile; and i cant understand why...
> 
> my question:
> I had to change most of my type of the functions.as is propagated through the code. (i.e. f need g; that need h; etc...) to add IO.
> is that kind of propagation normal, or can i Stop it somewhere ? how?

You can stop it at the very beginning:

f :: String -> String
g :: [A] -> [B]         -- etc, any functions without IO

parse :: [String] -> [ A ]

main = do
	h <- openFile "sampleFile" ReadMode
	contents <- hGetContents h
-- and now you have file contents binded to contents and you can apply your
--functions any way you want, i. e.
	let result = g (parse contents)
	putStrLn (show result)

Wojciech Moczydlowski, Jr