【发布时间】:2014-07-27 10:29:22
【问题描述】:
我有一小段代码要在 if 语句中使用。代码是
getRandomInt :: (Int, Int) -> IO Int
getRandomInt (a, b) = do
g <- newStdGen
return . fst $ randomR (a, b) g
然后再往下……
if ((getRandomInt (0,1)) >>= (\x -> x == 1))
根据我的理解,这应该编译成 randomInt == 1 并且可以在 if 语句中使用。反而;我收到此错误:
word.hs:30:21:
Couldn't match expected type `Bool' with actual type `IO b0'
In the expression: ((getRandomInt (0, 1)) >>= (\ x -> x == 1))
In a stmt of a 'do' block:
if ((getRandomInt (0, 1)) >>= (\ x -> x == 1)) then
getRandomVowel : (getWord $ len - 1)
else
getRandomConsonant : (getWord $ len - 1)
In the expression:
do { if ((getRandomInt (0, 1)) >>= (\ x -> x == 1)) then
getRandomVowel : (getWord $ len - 1)
else
getRandomConsonant : (getWord $ len - 1) }
很抱歉造成任何明显的误解,我昨天才开始学习 Haskell。
【问题讨论】: