On 2009/06/09, at 19:33, Tobias Olausson wrote:

You can not convert an IO Int to Int, or at least, you shouldn't.
However, you can do as follows:

test :: IO ()
test = do
   int <- randomRIO -- or whatever it is called
   print $ useInt int

useInt :: Int -> Int
useInt x = x+10

Or, you can lift pure function into IO. the below test' function almost same as above test function. (But I used randomIO instead of randomRIO because it seemed to be a typo :-)

    test' = print =<< fmap useInt randomIO

I think it is more handy than using do notation, when you want to do something simple with monads. And converting IO Int to IO anything is much easier and safer than converting IO Int to Int.

    ghci> :m +System.Random Data.Char
    ghci> :t fmap (+1) randomIO
    fmap (+1) randomIO :: (Num a, Random a) => IO a
    ghci> :t fmap show randomIO
    fmap show randomIO :: IO String
    ghci> :t fmap chr randomIO
    fmap Data.Char.chr randomIO :: IO Char
    ghci> :t fmap (+) randomIO
    fmap (+) randomIO :: (Num a, Random a) => IO (a -> a)

Thanks,
Hashimoto


//Tobias

2009/6/9 ptrash <ptr...@web.de>:

Hi,

I am using the System.Random method randomRIO. How can I convert its output
to an Int?

Thanks...
--
View this message in context: http://www.nabble.com/Convert-IO-Int- to-Int-tp23940249p23940249.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
Tobias Olausson
tob...@gmail.com
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to