2017-01-12 11 views
1

當我嘗試使用ghci中載入我的代碼,它給了我這個錯誤:哈斯克爾誤差在約束非類型變量參數:民[C]

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 
+0

如果添加了一個類型簽名'F'(也許'[INT] - > [Int] - > Int - >(Int,Int)'),那麼您將幫助編譯器瞭解程序的哪個部分是錯誤的,並且您可能會得到更有幫助的錯誤。 –

回答

5

因爲你使用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