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は、あなたがから減算しようとしていると考えて3おり、それは数値ではないabsと不平を言っています。abs単項否定演算子を使用する場合は、括弧を追加する必要があります。

abs (-3)
于 2011-06-03T07:28:58.840 に答える
5

abs - 3通訳はあなたがそうではないと思っていますabs (-3)。コードの曖昧さを解消し、減算演算子ではなく単項「-」関数を使用することを明確にするために、括弧が必要です。

于 2011-06-03T07:30:52.877 に答える