Reputation: 479
I'm trying to get a alternative permutation of two list with no fixed length for example:
x = ["a","b","c"]
y = [1,2,3]
should return:
[(a,1),(b,2),(c,3)] , [(a,2),(b,3),(c,1)], [(a,3),(b,1),(c,2)]
with list comprehensions I was able to get this:
[(x,y) | x<-x, y<-y ]
[("a",1),("a",2),("a",3),("b",1),("b",2),("b",3),("c",1),("c",2),("c",3)]
which is not exactly what I want
Upvotes: 0
Views: 426
Reputation: 27626
You only need to permute one of your two lists, then pair up each permutation with the other list in the original order:
import Data.List (permutations)
bijections :: [a] -> [b] -> [[(a, b)]]
bijections xs ys = map (zip xs) (permutations ys)
What this does is it generates all permutations of ys
(so that would be [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]
for your example) and then for each of them, it pairs them up with xs
in its original order.
Upvotes: 4