2016-07-09 29 views
2

我想在同一遍中映射多個列表。這種模式有沒有名字?是否有'地圖'多個列表的名稱?

從本質上講,我找的基本上是類似於這樣的名字:

mapMultiple :: (([a,b]) -> c) -> [[a],[b]] -> [c]

但希望可以有兩個以上的列表(我的Haskell是生鏽的,我不知道如何寫這種類型的簽名)

對於更具體的東西,比方說我有名單AB這樣的:

A = [1, 2, 3, 4] and B = ['A', 'B', 'C', 'D']

我希望能夠映射像這樣一個清單,B:

mapMultiple(([num, letter]) => ([ num, letter ]), [A, B]) == [[1,'A'], [2,'B'],[3,'C'],[4,'D']]

在粗糙的僞代碼,這裏是你將如何實現它:

mapMultiple = (fn, lists) => map(fn, zip(lists)) 

我尋找這種模式/功能的通用名稱。如果

獎勵積分你知道它的實現名稱(或者如果它沒有實現)在Ramda.js

+0

絕對'zipWith'。作爲一種方法:'let xs = [1,2,3,4],ys = ['A','B','C','D']; xs.map((x,i)=> [x,ys [i]])'或作爲curried函數:'const zipWith = ys => xs => xs.map((x,i)=> [x ,ys [i]]); zipWith(YS)(XS);'。雖然我沒有ramda解決方案。 – ftor

回答

4

也許你正在尋找zipWith,這是ZIP +地圖哈斯克爾:

> zipWith (*) [1,2,3] [4,5,6] 
[4,10,18] 

這是available in Rambda了。

或者zipList3爲3所列出:

> zipWith3 (\a b c -> a+2*b+3*c) [1,2,3] [4,5,6] [7,8,9] 
[30,36,42] 

ZipList應用性可以推廣到任意(靜態已知)號碼列表,在更冗長的價格:

> (\a b c d -> a+2*b+3*c+4*d) <$> ZipList [1,2,3] <*> ZipList [4,5,6] <*> ZipList [7,8,9] <*> ZipList [10,11,12] 
ZipList {getZipList = [70,80,90]} 
4

mapMultiple :: (([a,b]) -> c) -> [[a],[b]] -> [c]

在Haskell,[] :: * -> *即它需要一個類型並返回一個類型。因此,[] [a] [b][[a],[b]]沒有意義。您可能打算使用2元組。

mapMultiple :: ((a, b) -> c) -> ([a], [b]) -> [c]

在這種情況下,看zip :: [a] -> [b] -> [(a, b)]。您可能正在尋找的是map f . uncurry zip

相關問題