<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">This works:<br><br>Prelude System.Random> do { randomRIO (1,6) >>= (\x -> putStrLn $ "Value = " ++ show x) }<br>Value = 5<br><br>So does this:<br><br>Prelude System.Random> do { x <- randomRIO (1,6); putStrLn $ "Value = " ++ show x }<br>Value = 2<br><br>But not this:<br><br>1 import Control.Monad<br>2 import System.Random<br>3<br>4 foo :: IO ()<br>5 foo = do<br>6 x <- randomRIO (1,6)<br>7 putStrLn $ "Value = " ++ show x<br><br><br>foo.hs:6:18:<br> Ambiguous type variable `t' in the constraints:<br> `Num t' arising from the literal `1' at foo.hs:6:18<br> `Random t' arising from a use of `randomRIO' at foo.hs:6:7-21<br> Probable fix: add a type signature that fixes these type variable(s)<br>Failed, modules loaded:
none.<br>Prelude System.Random> <br><br>Or this:<br><br>1 import Control.Monad<br>2 import System.Random<br>3<br>4 foo :: IO ()<br>5 foo = randomRIO (1,6) >>= (\x -> putStrLn $ "Value = " ++ show x)<br><br><br>foo.hs:5:17:<br> Ambiguous type variable `t' in the constraints:<br> `Num t' arising from the literal `1' at foo.hs:5:17<br> `Random t' arising from a use of `randomRIO' at foo.hs:5:6-20<br> Probable fix: add a type signature that fixes these type variable(s)<br>Failed, modules loaded: none.<br><br><br>How to fix?<br><br>Michael<br><br><br><br><br><br></td></tr></table><br>