3

これが負の入力でクラッシュする理由を誰か説明できますか?

add :: Integral a => (a -> a) -> a -> a
add f n | n<0  = error "only non-negative integers allowed as input"
    | otherwise     = sum[ f x |x<-[1..n] ]


foo:: Int -> Int
foo x = x

*Main> add foo -5

<interactive>:82:9:
No instance for (Num (Int -> Int))
arising from a use of `-'
Possible fix: add an instance declaration for (Num (Int -> Int))
In the expression: add foo - 5
In an equation for `it': it = add foo - 5
4

1 に答える 1

9

(-5)unary を使用していることを明確にするために書く必要があります。-そうしないと、haskell は-二項演算子として読み取ります。

add foo (-5)
于 2013-11-10T23:53:03.280 に答える