2

我需要使用该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
4

3 回答 3

8

看来你输入了

ghci> pounds 45

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

ghci> del 45

那里,或

ghci> pounds [45]

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

于 2012-10-18T21:28:26.823 回答
4

pounds仅适用于列表,但您将其用于数字。

pounds [45]

可以正常工作。

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

于 2012-10-18T21:29:24.470 回答
2

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

尝试做pounds [45]

于 2012-10-18T21:27:59.977 回答