我正在使用ghci,並且遇到了用於獲取數字因子的函數的問題。Haskell函數在使用數字時有效,但與變量無關
我想工作的代碼是:
let factors n = [x | x <- [1..truncate (n/2)], mod n x == 0]
我努力(在這種情況下,與66)來使用它,它不抱怨,當我按下回車鍵,但只要我得到這個錯誤信息:
Ambiguous type variable 't0' in the constraints:
(Integral t0)
arising from a use of 'factors' at <interactive>:30:1-10
(Num t0) arising from the literal '66' at <interactive>:30:12-13
(RealFrac t0)
arising from a use of 'factors' at <interactive:30:1-10
Probable fix: add a type signature that fixes these type variable(s)
In the expression: factors 66
In the equation for 'it': it = factors 66
下面的代碼工作完美:
let factorsOfSixtySix = [x | x <- [1..truncate (66/2)], mod 66 x == 0]
我是新來的Haskell,並查找類型和ty之後佩奇,我還不確定我的意思。
猜猜'n/2'會返回什麼! – devnull