2012-10-18 51 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] 

会正常工作。

通常当编译器说它缺少一个实例时,它通常意味着你的参数是错误的类型或丢失。