Haskell error约束中的非类型变量参数:Num[c]
当我尝试使用ghci加载代码时,会出现以下错误:Haskell error约束中的非类型变量参数:Num[c],haskell,Haskell,当我尝试使用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 [
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
具有以下签名:
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
如果您向
f
(可能是[Int]->[Int]->[Int]->Int->(Int,Int)
)添加类型签名,那么您将帮助编译器了解程序的哪一部分是错误的,并且您可能会得到一个更有用的错误。
module Asig1 where
f as ys x = (s,s)
where
ws = zipWith (*) as ys
s = foldl (+) x ws --instead of foldl (+) ws x