Hi I am supposed to be learning Prolog and my teacher asked us to create a predicate. We are asked to make a predicate called compose which acts like zip but can take two input lists of uneven lengths. If one is bigger than the other, it simply appends the remaining excess elements of the larger list to the end of the result. The behavior as specified by the teacher is like this:
?- compose([a,b,c],[x,y,z],L).
L = [a,x,b,y,c,z]
?- compose([a,b,c],[x,y],L).
L = [a,x,b,y,c]
?- compose(L1,L2,[a,b,c]).
L1 = [] L2 = [a,b,c] ;
L1 = [a] L2 = [b,c] ;
L1 = [a,c] L2 = [b];
L1 = [a,b,c] L2 = []
My predicate so far :
my_compose([], [], []).
my_compose(List1, [], R):-
append([], List1, R).
my_compose([], List2, R):-
append([], List2, R).
my_compose([Item1|Tail1], [Item2|Tail2], [Item1, Item2|R]):-
my_compose(Tail1, Tail2, R).
But my output is somewhat different:
?- my_compose([a,b,c], [x,y,z], L).
L = [a, x, b, y, c, z] ;
L = [a, x, b, y, c, z] ;
L = [a, x, b, y, c, z].
?- my_compose([a,b,c], [x,y], L).
L = [a, x, b, y, c] ;
false.
?- my_compose(L1, L2, [a,b,c]).
L1 = [a, b, c],
L2 = [] ;
L1 = [],
L2 = [a, b, c] ;
L1 = [a, c],
L2 = [b] ;
L1 = [a],
L2 = [b, c] ;
false.
I would appreciate it deeply if anyone could explain what I am missing here?
Edit: The difference between this and shuffle is that shuffle is for two lists of the same length. Also, this should backward query.