在负输入Haskell上崩溃

时间:2013-11-10 23:51:10

标签: haskell

任何人都可以解释为什么这会在负面输入时崩溃吗?

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

1 个答案:

答案 0 :(得分:10)

你必须写(-5)来表明你正在使用一元-,否则haskell会将-作为二元运算符读取:

add foo (-5)