2015-07-10 123 views
1

我一直在尋找上帝知道多久,但不能得到這段代碼的工作:哈斯克爾錯誤

solution n = 3/2*((n-n`mod`3)+3) 

它編譯,但是當我叫它,它拋出兩個錯誤,第一個是

No instance for (Integral a0) arising from a use of `solution' 

這裏是我想要達到

solution 9 = 3/2* ((n-n`mod`3)+3) = 3/2 * ((9-0)+3) = 3/2 * (12) = 18 

回答

2

哈斯克爾不會讓你添加Fractional3/2)到Integral

((n - n `mod` 3) + 3) 

沒有明確告訴它,你加入了fromIntegral後者希望。所以

solution n = 3/2 * fromIntegral ((n-n`mod`3)+3) 

應該工作。

Real World haskell中的所有數字類型都有很好的概述。我從來沒有記住過細節,大多數情況下,只要需要,就添加fromIntegral