map2採用兩個列表ls1和ls2以及一個函數F,並返回一個與ls1和ls2中最短長度相同的列表,其中第i個元素是將F到LS1和LS2的第i個要素的結果(按順序)Haskell中的高階函數 - 「沒有......的實例」
module Map2 where
map2 lst1 lst2 f = map2_iter lst1 lst2 f len 0 []
where len = (min (length lst1), (length lst2))
map2_iter (x:lst1) (y:lst2) f len i acc = if (i == len)
then acc
else let res = (f x y) in
map2_iter (lst1) (lst2) (f) (len) (i+1) ((res):acc)
map2_iter [] [] f len i acc = []
我有以下錯誤
Map2.hs:3:20:
No instances for (Eq (Int -> Int), Num (Int -> Int, Int))
arising from a use of `map2_iter'
Possible fix:
add instance declarations for
(Eq (Int -> Int), Num (Int -> Int, Int))
In the expression: map2_iter lst1 lst2 f len 0 []
In an equation for `map2':
map2 lst1 lst2 f
= map2_iter lst1 lst2 f len 0 []
where
len = (min (length lst1), (length lst2))
我真的不知道這個錯誤意味着什麼。任何人都可以提供幫助嗎?
此外,這不是硬件,而是測試準備。
當你得到這個工作時,你會發現結果是相反的。與'zipWith'不同的是,它確實可以做你認爲你的功能所做的事情。但是在你使用這個工具之前,只要你使用不同長度的列表,你也會遇到模式匹配錯誤。 – Ingo 2013-02-28 18:15:47