gpt4 book ai didi

haskell - 在负输入 Haskell 上崩溃

转载 作者:行者123 更新时间:2023-12-01 08:31:02 25 4
gpt4 key购买 nike

谁能解释为什么这会在负输入时崩溃?

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

最佳答案

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

add foo (-5)

关于haskell - 在负输入 Haskell 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19896754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com