我不明白問題是什麼。 'a'不是一個bool,不應該是一個bool。那麼爲什麼布爾有望?Haskell錯誤:無法匹配預期的類型`Bool'與推斷的類型`IO b'
代碼:
probablyPrime n 0 = False
probablyPrime n t =
do a <- randomRIO(3, n-1 :: Integer)
let comp = defComp(a,n)
let ret = (not comp) && (probablyPrime n t-1)
return ret
defComp a n = xcon1 && xcon2
where (s,m) = findsm n
x = a^m `mod` n
xcon1 = x /= 1 || x /= n-1
xcon2 = comploop x n s
comploop x n 0 = False
comploop x n s = x1 || (comploop x n (s-1))
where x1 = (x^2 `mod` n) == 1
findsm n = (s,m)
where m = findm n
s = n/m
findm n = m
where f = (logBase 2 n) - (truncate (logBase 2 n))
m' = 2^f
m = m_ify m'
m_ify m | m mod 1 == 0 = m
| otherwise = m_ify (m*2)
錯誤:
Couldn't match expected type `Bool' against inferred type `IO b'
In a stmt of a 'do' expression:
a <- randomRIO (3, n - 1 :: Integer)
In the expression:
do { a <- randomRIO (3, n - 1 :: Integer);
let comp = defComp ...;
let ret = (not comp) && (probablyPrime n t - 1);
return ret }
In the definition of `probablyPrime':
probablyPrime n t
= do { a <- randomRIO (3, n - 1 :: Integer);
let comp = ...;
let ret = ...;
.... }
你需要小心你的函數調用語法。你可以使用`f(x,y)`幾次,但是這在Haskell中幾乎是不對的 - 它將函數應用於單個元組;你想`f x y`。 – 2010-12-01 14:25:32
(實際上,更確切地說,你*希望它爲'randomRIO`而不是'defComp`) – 2010-12-01 14:59:40
這裏的邏輯沒有一個基本的問題嗎?如果'&'-ing`可能是't`一直到`t = 0`和`probablyPrime n 0`爲`False`,那麼你總是會得到`False`。 – 2010-12-01 15:34:16