2012-10-18 81 views
2

我需要使用map函數來獲得例如便士到磅的轉換。 對不起,這個愚蠢的問題..但我是一個初學者。詮釋到浮點轉換:沒有實例爲數[Int]

del :: Int -> Float 
del x = (fromIntegral x)/100 

pounds :: [Int] -> [Float] 
pounds = map del 

,我得到這個錯誤..

*Main> pounds 45 
<interactive>:90:8: 
    No instance for (Num [Int]) 
     arising from the literal `45' 
    Possible fix: add an instance declaration for (Num [Int]) 
    In the first argument of `pounds', namely `45' 
    In the expression: pounds 45 
    In an equation for it': it = pounds 45 

回答

8

看來你在提示符下鍵入

ghci> pounds 45 

。但是pounds預計列表(Int)作爲它的參數。您應該使用

ghci> del 45 

那裏,或者

ghci> pounds [45] 

由於整數文字有一個隱含的fromInteger,GHC嘗試查找轉換fromInteger :: Integer -> [Int],這需要一個instance Num [Int],但它無法找到一個,那是它報告的錯誤。

2

pounds需要的參數是一個列表Int,不是一個孤立的Int

改爲嘗試使用pounds [45]

4

pounds只適用於列表,但您在數字上使用它。

pounds [45] 

會正常工作。

通常當編譯器說它缺少一個實例時,它通常意味着你的參數是錯誤的類型或丟失。