6

遵循http://en.wikibooks.org/wiki/Haskell/Beginning中的示例

Prelude> let abs x = if x < 0 then -x else x
Prelude> abs 5
5
Prelude> abs -3

<interactive>:1:6:
    No instance for (Num (a0 -> a0))
      arising from the literal `3'
    Possible fix: add an instance declaration for (Num (a0 -> a0))
    In the second argument of `(-)', namely `3'
    In the expression: abs - 3
    In an equation for `it': it = abs - 3

怎么了?

4

2 回答 2

14

Haskell 认为您正在尝试从 中减去3abs并抱怨这abs不是一个数字。使用一元否定运算符时需要添加括号:

abs (-3)
于 2011-06-03T07:28:58.840 回答
5

口译员认为你的意思abs - 3不是abs (-3)。您需要括号来消除代码歧义,并确保您打算使用一元“-”函数,而不是减法运算符。

于 2011-06-03T07:30:52.877 回答