2014-03-12 51 views
0

你好,我最近開始學習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] 

+1

有什麼錯誤? – Khaelid

+1

'my_list'有5個參數,但你只提供4. – Lee

+0

@Khaelid我編輯了這個問題。我複製了我的代碼的錯誤版本。 – JmRag

回答

3

my_sum需要一個參數,一個數字列表。由於my_list返回一個列表,其包裹在結果列表結果列表的列表(不匹配my_sum):

my_sum [my_list f 1 1 5] -- argument has type Num a => [[a]] 
my_sum (my_list f 1 1 5) -- this is right 
+0

原始'myList'只有四個參數。 – Yuuri

+0

@Yuuri編輯匹配。 –