I have an interesting problem to figure out involving nested list comprehensions. I will redact unnecessary details for this post, as such the problem I'm trying to solve may seem a little peculiar.
I have two tuples as follows ("X", "Y", "Z")
and ("1", "2")
. The contents of these will only ever be as written above.
Using these two lists, I need to construct a list of 3-tuples, with every combination of the contents of the above two lists. An example of one of these tuples would be ("X2", "Z2", "Y1)
. Note the numbers always come after the letters.
I know I need to use a list comprehension for this, but it seems like I need to have a nested list comprehension somehow, which I am not familiar with.
I started with this:
[(comb1, comb2, comb2) | {- What goes here? -}]
I am not sure how to proceed. How can I solve this problem with a nested list comprehension? I believe a nested list comprehension is the best way to solve this problem but if you know of a better way please explain that instead.
EDIT with some further details:
This list of tuples will be output by a function that would look something like this:
init :: [(String, String, String)]
init = [(comb1, comb2, comb3) | {- What goes here? -}]
The return value from the init function will be used in the context of a larger program that will need to perform tasks on this list of 3-tuples. The actual two tuples, ("X", "Y", "Z")
and ("1", "2")
, will be hardcoded into the list comprehension of the init
function.
There are more combinations than just what a cartesian product gives. The cartesian product would result in [["X1","Y1","Z1"],["X2","Y2","Z2"]]
. However, the letters need not be in order, and some letters might not appear in one of the results at all. ["Z1", "X2", "X1"]
is also a valid combination