[Haskell-cafe] so how does one convert an IO a into an a ?

Crypt Master cryptmaster at hotmail.com
Thu Jul 8 14:50:44 EDT 2004


Thanks for your help so far, but I am still not getting this IO stuff. After 
reading your previous help and reading several articles on it I still cant 
phathom how you convert the IO Int into an Int.

One person mentioned how random just returns an interative program which 
when eveluated returns the Int. Also from the school of expression book he 
says " The right way to think of (>>=) above is simply this: It "Executes" 
e1 ..." in relation to "do pat <- e1 ...".

so I have this:

rollDice :: IO Int
rollDice = getStdRandom (randomR (1,6))

rl :: [Int]
rl = [ (getRndNum x) | x <- [1..] ]

getRndNum :: Int -> Int
getRndNum x = do n <- rollDice
               return n
</code>  *PS Pretend return is correctly aligned under n. dont what ahppens 
in copy and paste*

now my understanding therefore is that "do n <- rollDice" should execute the 
the itererative program returned by rollDice. So now n should be my Int 
since IO Int was a program which when evaluted returns an Int ?

However this is what haskell thinks of my thoery:

*** Term           : getRndNum
*** Type           : Int -> IO (Maybe Int)
*** Does not match : Int -> Int

So I am still in IO Int land despite having used the >>= in the do syntax. 
Worse Still I am in IO (Maybe Int) land. Monads within Monads.

In yours, and many other examples I found online, the results are always 
passed to print which seems to know how to deal with an IO Int. Is this 
specially coded or overloaded or something ?

There are plenty of examples which use return like so:

do k <- getKey w
    return k

which is what I tried above to no avail.

It seems awefully complicated just to get hold a simple Int, but naturally 
complicity is directly related to ones understanding. Mine is sumewhat 
lacking ... any help would be appreciated.



Tired of spam? Get advanced junk mail protection with MSN 8. 

More information about the Haskell-Cafe mailing list