我试图编译在Haskell下面的函数来模仿多项式,其常数的数值列表中指定的分化:为什么Haskell将我的Num类型解释为Enum?
diff :: (Num a) => [a] -> [a]
diff [] = error "Polynomial unspecified"
diff coeff = zipWith (*) (tail coeff) [0..]
哈斯克尔拒绝编译它,给我的理由如下:
Could not deduce (Enum a) from the context (Num a)
arising from the arithmetic sequence `0 .. ' at fp1.hs:7:38-42
Possible fix:
add (Enum a) to the context of the type signature for `diff'
In the third argument of `zipWith', namely `[0 .. ]'
In the expression: zipWith (*) (tail coeff) ([0 .. ])
In the definition of `diff':
diff coeff = zipWith (*) (tail coeff) ([0 .. ])
为什么Haskell的治疗[0..]
列表作为枚举类型,我怎么能解决这个问题。请记住,我想利用这里的懒惰评估,因此无限的名单。
岂不'DIFF [] = []'或'的diff [_] = [0]'更好?毕竟,x^2 + 1的第四个微分应该是一个常数0,而不是一个错误。 – ephemient 2009-10-07 19:41:46
@ephemient:你是鹰眼!我在发布问题后处理单例列表异常。该代码还应该读取'[1 ..]'而不是'[0 ..]'来区分它。 – Zaid 2009-10-07 19:50:58