列表的所有元素*列表

时间:2019-04-04 03:52:21

标签: haskell

我正在尝试用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

1 个答案:

答案 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]