我正在尝试用2个这样的列表来做元组列表:
["a", "b", "c"] ["a", "b", "c"]
对此:
[("a", "a"), ("a", "b"),("a", "c")], [("b", "a"), ("b", "b"), ("b", "c"), ("c", "a"), ("c", "b"), ("a", "c")]
所以我正在尝试,但是我不知道该如何做:
f [] [] = [[]]
f (x:xs) y =
zip (repeat x) y: f xs y
答案 0 :(得分:6)
要进行显式递归,很难击败:
f :: [a] -> [a] -> [[(a, a)]]
f [] _ = []
f _ [] = []
f (x:xs) yss = go x yss : f xs yss
where
go _ [] = []
go x (y:ys) = (x, y): go x ys
但是没有理由这样做。这是列表理解的用例!
f xs ys = [[(x, y) | y <- ys] | x <- xs]