Int 到 Float 转换:Num [Int] 没有实例



我需要使用map函数来获得例如便士到英镑的转换。 对不起,这个愚蠢的问题..但我是初学者。

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

似乎你输入了

ghci> pounds 45

在提示符下。但pounds期望一个列表(Int)作为它的论据。您应该使用

ghci> del 45

那里,或

ghci> pounds [45]

由于整数文字有一个隐式fromInteger,GHC试图找到转换fromInteger :: Integer -> [Int],这需要一个instance Num [Int],但它找不到,这是它报告的错误。

pounds仅适用于列表,但您在数字上使用了它。

pounds [45]

会正常工作。

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

pounds的参数需要是一个Int列表,而不是一个孤立的Int

试着做pounds [45]

最新更新