Newbied question on IO Monad

Hello all, update :: String -> String update sss = ... main = do writeFile "myFile.txt" sss x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz I know that function main returns IO String, function update returns String. And my purpose is to get the string zzz from main for the value return of function update. But I do not know which way I can do. Sorry if the question seems ridiculous. Thanks for any help. S.

On Tue, 12 Sep 2006, Sara Kenedy wrote:
update :: String -> String update sss = ...
main = do writeFile "myFile.txt" sss x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz
I know that function main returns IO String, function update returns String. And my purpose is to get the string zzz from main for the value return of function update. But I do not know which way I can do.

Il Tue, Sep 12, 2006 at 08:05:42AM -0400, Sara Kenedy ebbe a scrivere:
Hello all,
update :: String -> String update sss = ...
main = do writeFile "myFile.txt" sss x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz
I know that function main returns IO String, function update returns String. And my purpose is to get the string zzz from main for the value return of function update. But I do not know which way I can do.
Did you mean something like this? update :: String -> String update sss = "Hi! " ++ sss main = do writeFile "myFile.txt" $ update "What are you trying to do?" x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz In this case main :: IO String so you will not see any output (quite useless). I'd suggest you to have a look at this tutorial that explain quite well the IO Monad: http://haskell.org/haskellwiki/IO_inside Ciao Andrea

Hello,
Maybe what I talk is not clear.
I want to take the input string sss of "update" to use in: writeFile
"myFile.txt" sss of function "main" and get the value zzz from "main"
to assign for the value return of "update". I think I need to change
the way to declare two functions to get what I want, but I do not know
how.
update :: String -> String
update sss = zzz
main = do writeFile "myFile.txt" sss
x <- callSystem "myFile.txt"
y <- openFile "result.txt" ReadMode
zzz <- hGetContents y
return zzz
S.
On 9/12/06, Andrea Rossato
Il Tue, Sep 12, 2006 at 08:05:42AM -0400, Sara Kenedy ebbe a scrivere:
Hello all,
update :: String -> String update sss = ...
main = do writeFile "myFile.txt" sss x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz
I know that function main returns IO String, function update returns String. And my purpose is to get the string zzz from main for the value return of function update. But I do not know which way I can do.
Did you mean something like this?
update :: String -> String update sss = "Hi! " ++ sss
main = do writeFile "myFile.txt" $ update "What are you trying to do?" x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz
In this case main :: IO String so you will not see any output (quite useless). I'd suggest you to have a look at this tutorial that explain quite well the IO Monad: http://haskell.org/haskellwiki/IO_inside
Ciao Andrea _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Sara,
I want to take the input string sss of "update" to use in: writeFile "myFile.txt" sss of function "main" and get the value zzz from "main" to assign for the value return of "update". I think I need to change the way to declare two functions to get what I want, but I do not know how.
update :: String -> String update sss = zzz
main = do writeFile "myFile.txt" sss x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz
I do not quite get what you want to accomplish. Is it something like this? update :: String -> IO String update s = do writeFile "myFile.txt" sss callSystem "myFile.txt" z <- readFile "result.txt" return z Note that update produces a value of type IO String now: in Haskell 98, there is no escape from the IO monad. Chances are that the above is not what you want. Can you then please explain one more time what it is what you're after? Cheers, Stefan

On Tue, Sep 12, 2006 at 09:00:19AM -0400,
Sara Kenedy
Maybe what I talk is not clear.
Do not worry, not all the haskell-cafe readers speak english. (I'm french, for instance.)
I want to take the input string sss of "update" to use in: writeFile "myFile.txt" sss of function "main" and get the value zzz from "main" to assign for the value return of "update".
From a newbie to another newbie: I do not think it is possible. "There is no way out of the IO monad, once you're in it."
See the explanations in the page indicated by Henning Thielemann : http://haskell.org/hawiki/ThatAnnoyingIoType This should work (not tested): update :: String -> IO String update sss = do writeFile "myFile.txt" sss x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz main = update "Foobar"

Il Tue, Sep 12, 2006 at 09:00:19AM -0400, Sara Kenedy ebbe a scrivere:
Hello,
Maybe what I talk is not clear.
I want to take the input string sss of "update" to use in: writeFile "myFile.txt" sss of function "main" and get the value zzz from "main" to assign for the value return of "update". I think I need to change the way to declare two functions to get what I want, but I do not know how.
update :: String -> String update sss = zzz
main = do writeFile "myFile.txt" sss x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz
S.
this is what you are trying to do with this code: 1. open a file and write to it an undefined string called "sss" 2. binding x with the value of a function name callSystem that takes a string (we do not know what it returns because it's undefined in this piece of code, but it must be a string). 3. open a file, "result.txt", read its content and put it in the IO Monad. Instead you would like to insert into the file myFile.txt a string, sss, that is the result of applying a function to the content of "result.txt". Am I right? If yes, here some code: update :: String -> String update sss = "This is the content of result.txt:\n" ++ sss main = do y <- openFile "result.txt" ReadMode zzz <- hGetContents y writeFile "myFile.txt" $ update zzz putStrLn "Done!" return () Ciao Andrea
participants (6)
-
Andrea Rossato
-
Henning Thielemann
-
Neil Mitchell
-
Sara Kenedy
-
Stefan Holdermans
-
Stephane Bortzmeyer