![](/img/trans.png)
[英]Haskell Error: Non type-variable argument in the constraint: Num (a -> a -> a -> a)
[英]Haskell error Non type-variable argument in the constraint: Num [c]
当我尝试使用ghci加载代码时,出现此错误:
Asig1.hs:4:1:
Non type-variable argument in the constraint: Num [c]
(Use FlexibleContexts to permit this)
When checking that ‘f’ has the inferred type
f :: forall c (t :: * -> *).
(Num c, Num [c], Foldable t) =>
[c] -> [c] -> t [c] -> ([c], [c])
我不明白我做错了什么。 这是我的代码:
module Asig1 where
f as ys x = (s,z)
where
ws = zipWith (*) as ys
s = foldl (+) ws x
z = s
因为您使用foldl
可能是错误的方式。 foldl
具有签名:
Foldable t => (b -> a -> b) -> b -> t a -> b
因此,您向它提供了一个函数(此处为(+)
)初始值和一系列值。 您可以使用以下方法修复代码:
module Asig1 where f as ys x = (s,z) where ws = zipWith (*) as ys s = foldl (+) x ws --instead of foldl (+) ws x z = s
您可以进一步改善代码,因为z = s
是不必要的:
module Asig1 where f as ys x = (s,s) where ws = zipWith (*) as ys s = foldl (+) x ws --instead of foldl (+) ws x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.