2013-12-12 65 views
1

我有兩個列表x = ["a","b","c"]y = ["Argentina","Brazil","Canada"]。我想要一個列表,如[["a","Argentina"],["b","Brazil"],["c","Canada"]]。 任何人都可以幫我嗎?謝謝。如何在Haskell中連接兩個列表

+0

看起來當然功課。 –

回答

4

我不確定是否有更簡單的方法,但可以使用zip生成元組列表,然後使用map返回列表列表。

Prelude> map (\(a,b) -> [a,b]) $ zip ["a","b","c"] ["Argentina","Brazil","Canada"] 
[["a","Argentina"],["b","Brazil"],["c","Canada"]] 

編輯:丹尼爾·馬丁指出,zipWith更簡單。

Prelude> zipWith (\a b -> [a,b]) ["a","b","c"] ["Argentina","Brazil","Canada"] 
[["a","Argentina"],["b","Brazil"],["c","Canada"]] 
+5

或者跳過'map'並使用'zipWith'。 –

3

只需使用

zipWith (\x y -> [x,y]) 
8

沒有看到您的使用情況下,我猜想,元組其實就足夠了,而且更類型安全的,因此,這只是zip

x = ["a","b","c"] 
y = ["Argentina","Brazil","Canada"] 
z = zip x y 

Prelude> z 
[("a","Argentina"),("b","Brazil"),("c","Canada")]