[英]Why does Haskell interpret my Num type as an Enum?
我正在尝试在Haskell中编译以下函数,以模仿在数值列表中指定常量的多项式的区分:
diff :: (Num a) => [a] -> [a]
diff [] = error "Polynomial unspecified"
diff coeff = zipWith (*) (tail coeff) [0..]
Haskell拒绝编译它,给出了以下原因:
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..]
列表视为Enum类型,我该如何解决这个问题。 请记住,我想利用这里的懒惰评估,因此无限列表。
[0..]
是enumFrom 0
语法糖,在Enum
类中定义。 因为要生成的列表a
以s [0..]
编译器需要a
要在类Enum
。
您可以将Enum a
添加到函数的类型签名中,也可以通过生成[0..] :: [Integer]
并使用fromInteger
(在类Num
定义)从中获取[a]
来解决该问题。 :
diff :: (Num a) => [a] -> [a]
diff [] = error "Polynomial unspecified"
diff coeff = zipWith (*) (tail coeff) (map fromInteger [0..])
必须有正确的diff
类型
diff :: (Num a, Enum a) => [a] -> [a]
因为[x..]
要求类型实例化Enum
。
[0..]
是enumFrom 0
缩写, 请参见此处
以下是编译器在查看此函数时看到的内容的快速摘要:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.