"没有实例"错误

zaf*_*zaf 6 haskell ghci

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
Run Code Online (Sandbox Code Playgroud)

怎么了?

ham*_*mar 14

哈斯克尔认为你想减3abs,并抱怨abs是不是一个数字.使用一元否定运算符时需要添加括号:

abs (-3)
Run Code Online (Sandbox Code Playgroud)


MGw*_*nne 5

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