I want to append a list of list like this : append(Ls,L)
,the first element of L is the first element of the first list in Ls,the second element of L is the first element of the second list in Ls, and so on for all the lists in Ls. After this, the next element of L is the second element of the first list in Ls, and so on, until two elements have been taken from all the lists in Ls. After this, come the third elements of all the lists in Ls, and so on, until all the elements of all the lists in Ls are included in L.
forexample:
I have a list Ls=[[a,b],[c,d],[e,f]]
and want to get this L=[a,c,e,b,d,f]
or I have Ls=[[1,2,3],[4,5,6]]
and I want to get L=[1,4,2,5,3,6]
If you want to practise implementing recursive predicates, you could proceed like this using dcg:
Above code is based on three auxiliary predicates which can be defined like this:
Let's run some queries! First, we use some quadratic matrix:
Next, the non-quadratic use-cases the OP gave:
Let's try going the "other direction", too!
This is not a relational solution, but illustrates a recursive approach to the problem:
The cut (
!
) eliminates the unneeded choice point.since append/2 has a different meaning, let's call it zip/2
If you use SWI-Prolog, you can proceed like this:
Here's the sample query the OP gave: