What is a smart way to find all possible combinations of these two sets of items?

Say I have two kinds of items

data Item1 = A | B | C data Item2 = D | E | F 

And two sets

 set1 = [A,B,C] set2 = [D,E,F] 

I would like to find all the unique ways of matching elements from two sets, the answer should be (in an unofficial post):

 AD,BE,CF AD,BF,CE AE,BD,CF AE,BF,CD AF,BD,CE AF,BE,CD 

In other words, I need some function that does the following:

 combine :: [Item1] -> [Item2] -> [[(Item1,Item2)]] combine = undefined 

Note that each combination must be a tuple, and each line in the enumeration scheme above must be a list, for example:

 [(A,D),(B,E),(C,F)] 
+6
source share
1 answer

Use this

 import Data.List (sort, permutations) combine as bs = zipWith zip (repeat as) (sort $ permutations bs) 
+7
source

Source: https://habr.com/ru/post/974700/


All Articles