I have a list of lists and I'd like to generate the possible permutations, the first element coming from the first sub-list, the second from the second sub-list and so on.
?- A = [[1], [a, b], [2]], magic_function(A, Ans).
Ans = [1, a, 2]
; Ans = [1, b, 2]
; false.
?- B = [[1], [a, b], [4, 5]], magic_function(B, Ans).
Ans = [1, a, 4]
; Ans = [1, a, 5]
; Ans = [1, b, 4]
; Ans = [1, b, 5]
; false
I suppose it's a pretty standard idea, but for some reason I just can't seem to come up with anything useful.
It can be done using maplist/3
and member/2
:
?- maplist(member, Es, [[1],[a,b],[2]]).
Es = [1,a,2]
; Es = [1,b,2].