[Haskell-beginners] IO vars
Ozgur Akgun
ozgurakgun at gmail.com
Wed Aug 29 11:43:22 CEST 2012
On 29 August 2012 10:21, Corentin Dupont <corentin.dupont at gmail.com> wrote:
> *f,g :: IO ()
> f = withFile "toto" WriteMode (flip hPutStr "42")
> g = withFile "toto" ReadMode hGetLine >>= (\s -> putStrLn $ "Answer:" ++ s)
> main = f >> g*
>
> Is it possible to do the same without files (the types must remain IO())?
>
One can use an IORef to get a similar effect.
import Data.IORef
import System.IO.Unsafe
{-# NOINLINE toto #-}
toto :: IORef String
toto = unsafePerformIO (newIORef "")
f,g :: IO ()
f = writeIORef toto "42"
g = readIORef toto >>= (\s -> putStrLn $ "Answer:" ++ s)
main = f >> g
HTH,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120829/b705a71c/attachment.htm>
More information about the Beginners
mailing list