Given a list of length lists x, where all the sublist letters are the same length y, print y^xlists of length xthat contain one item from each sublist.
Example ( x = 3, y = 2):
[ [1, 2], [3, 4], [5, 6] ]
Output ( 2^3 == 8different outputs):
[ [1, 3, 5], [1, 4, 5], [1, 3, 6], [1, 4, 6],
[2, 3, 5], [2, 4, 5], [2, 3, 6], [2, 4, 6] ]
My research / work
ruby
I wrote the actual code to complete this task, but in Ruby, as it is the most convenient language for me.
def all_combinations(lst)
lst.inject {|acc, new| acc.product(new).map(&:flatten) }
end
A type
Input is a list of lists containing elements of type a, etc. - output.
allProduct :: [[a]] -> [[a]]
Cartesian product, flattening and folding
Ruby, , . , , .