你好,我最近開始學習Haskell和我試圖做的是給它創建了一個功能列表的小程序,然後我想利用列表的總和:試圖從生成的列表總結
f a b c = a+b+c
my_sum [] = 0
my_sum (x:xs) = x + my_sum xs
my_list f a b c = [f a b x |x <- [1..c]]
我試圖採取列表的總和是這樣,但你能幫助我,我總是得到錯誤
*Main> my_sum [my_list f 1 1 4]
<interactive>:13:1:
No instance for (Num [t0]) arising from a use of `my_sum'
Possible fix: add an instance declaration for (Num [t0])
In the expression: my_sum [my_list f 1 1 4]
In an equation for `it': it = my_sum [my_list f 1 1 4]
<interactive>:13:9:
No instance for (Num t0) arising from a use of `my_list'
The type variable `t0' is ambiguous
Possible fix: add a type signature that fixes these type variabl
Note: there are several potential instances:
instance Num Double -- Defined in `GHC.Float'
instance Num Float -- Defined in `GHC.Float'
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus three others
In the expression: my_list f 1 1 4
In the first argument of `my_sum', namely `[my_list f 1 1 4]'
In the expression: my_sum [my_list f 1 1 4]
?
有什麼錯誤? – Khaelid
'my_list'有5個參數,但你只提供4. – Lee
@Khaelid我編輯了這個問題。我複製了我的代碼的錯誤版本。 – JmRag