projog

3.101. append(X,Y,Z) - concatenates two lists.

The append(X,Y,Z) goal succeeds if the concatenation of lists X and Y matches the list Z.

Examples

Examples of when all three terms are lists:

?- append([a,b,c], [d,e,f], [a,b,c,d,e,f]).

yes

?- append([a,b,c], [a,b,c], [a,b,c,a,b,c]).

yes

?- append([a], [b,c,d,e,f], [a,b,c,d,e,f]).

yes

?- append([a,b,c,d,e], [f], [a,b,c,d,e,f]).

yes

?- append([a,b,c,d,e,f], [], [a,b,c,d,e,f]).

yes

?- append([], [a,b,c,d,e,f], [a,b,c,d,e,f]).

yes

?- append([], [], []).

yes

?- append([a,b], [d,e,f], [a,b,c,d,e,f]).

no

?- append([a,b,c], [e,f], [a,b,c,d,e,f]).

no

?- append([W,b,c], [d,Y,f], [a,X,c,d,e,Z]).
W = a
X = b
Y = e
Z = f

yes

Examples of when first term is a variable:

?- append(X, [d,e,f], [a,b,c,d,e,f]).
X = [a,b,c]

yes;

no

?- append(X, [f], [a,b,c,d,e,f]).
X = [a,b,c,d,e]

yes;

no

?- append(X, [b,c,d,e,f], [a,b,c,d,e,f]).
X = [a]

yes;

no

?- append(X, [a,b,c,d,e,f], [a,b,c,d,e,f]).
X = []

yes;

no

?- append(X, [], [a,b,c,d,e,f]).
X = [a,b,c,d,e,f]

yes

Examples of when second term is a variable:

?- append([a,b,c], X, [a,b,c,d,e,f]).
X = [d,e,f]

yes

?- append([a,b,c,d,e], X, [a,b,c,d,e,f]).
X = [f]

yes

?- append([a], X, [a,b,c,d,e,f]).
X = [b,c,d,e,f]

yes

?- append([], X, [a,b,c,d,e,f]).
X = [a,b,c,d,e,f]

yes

?- append([a,b,c,d,e,f], X, [a,b,c,d,e,f]).
X = []

yes

Examples of when third term is a variable:

?- append([a,b,c], [d,e,f], X).
X = [a,b,c,d,e,f]

yes

?- append([a], [b,c,d,e,f], X).
X = [a,b,c,d,e,f]

yes

?- append([a,b,c,d,e], [f], X).
X = [a,b,c,d,e,f]

yes

?- append([a,b,c,d,e,f], [], X).
X = [a,b,c,d,e,f]

yes

?- append([], [a,b,c,d,e,f], X).
X = [a,b,c,d,e,f]

yes

?- append([], [], X).
X = []

yes

Examples of when first and second terms are variables:

?- append(X, Y, [a,b,c,d,e,f]).
X = []
Y = [a,b,c,d,e,f]

yes;
X = [a]
Y = [b,c,d,e,f]

yes;
X = [a,b]
Y = [c,d,e,f]

yes;
X = [a,b,c]
Y = [d,e,f]

yes;
X = [a,b,c,d]
Y = [e,f]

yes;
X = [a,b,c,d,e]
Y = [f]

yes;
X = [a,b,c,d,e,f]
Y = []

yes

?- append(X, Y, [a]).
X = []
Y = [a]

yes;
X = [a]
Y = []

yes

?- append(X, Y, []).
X = []
Y = []

yes

Examples when combination of term types cause failure:

?- append(a, b, Z).

no

?- append(a, b, c).

no

?- append(a, [], []).

no

?- append([], b, []).

no

?- append([], [], c).

no

?- append([], tail, Z).
Z = tail

yes

?- append([], Z, tail).
Z = tail

yes

?- append([a], b, X).
X = [a|b]

yes

?- append([a,b,c], d, X).
X = [a,b,c|d]

yes

?- append([a], [], X).
X = [a]

yes

?- append([a], [b], X).
X = [a,b]

yes

?- append([X|Y],['^'],[a,b,c,^]).
X = a
Y = [b,c]

yes;

no

?- append([X|Y],['^'],[a,b,c,^,z]).

no

?- append([X|Y],['^'],[a,b,c,^,z,^]).
X = a
Y = [b,c,^,z]

yes;

no

?- append([X|Y],['^'],[a,b,c,^,^]).
X = a
Y = [b,c,^]

yes;

no

?- append([a|b], [b|c], X).

no

?- append([a|b], [b|c], [a,b,c,d]).

no

?- append([a|b], X, [a,b,c,d]).

no

?- append(X, [b|c], [a,b,c,d]).

no

?- append([a|b], X, Y).

no

?- append([a, a], X, [a]).

no

?- append(X,[a,a],[a]).

no

?- append([a,a],X,[]).

no

?- append(X,[a,a],[]).

no

?- append(X,[a,a],[a,a]).
X = []

yes;

no

?- append([a,a],X,[a,a]).
X = []

yes

?- append(X,[],[a,a]).
X = [a,a]

yes

?- append([],X,[a,a]).
X = [a,a]

yes

?- append([],[],X).
X = []

yes

?- append(Left,[x|Right],[a,x,b,c,d,x,e,f]).
Left = [a]
Right = [b,c,d,x,e,f]

yes;
Left = [a,x,b,c,d]
Right = [e,f]

yes;

no

?- append(Left,[x,b|Right],[a,x,b,c,d,x,e,f]).
Left = [a]
Right = [c,d,x,e,f]

yes;

no

?- append([a|X],[a|Y],[a,a,a,a,a,a,a]).
X = []
Y = [a,a,a,a,a]

yes;
X = [a]
Y = [a,a,a,a]

yes;
X = [a,a]
Y = [a,a,a]

yes;
X = [a,a,a]
Y = [a,a]

yes;
X = [a,a,a,a]
Y = [a]

yes;
X = [a,a,a,a,a]
Y = []

yes;

no

?- append([a,a|X],[a|Y],[a,a,a,a,a,a,a]).
X = []
Y = [a,a,a,a]

yes;
X = [a]
Y = [a,a,a]

yes;
X = [a,a]
Y = [a,a]

yes;
X = [a,a,a]
Y = [a]

yes;
X = [a,a,a,a]
Y = []

yes;

no

?- append([a|X],[Y|[a]],[a,a,a,a,a,a,a]).
X = [a,a,a,a]
Y = a

yes;

no

?- append([X|[a]],Y,[a,a,a,a,a,a,a]).
X = a
Y = [a,a,a,a,a]

yes

?- append([X|[a]],[Y|[a]],[a,a,a,a,a,a,a]).

no

?- append([a,a,a],[a,a,a],[a|X]).
X = [a,a,a,a,a]

yes

?- append([a,b|X],[d,e|Y],Z).
X = []
Y = UNINSTANTIATED VARIABLE
Z = [a,b,d,e|Y]

yes;
X = [X]
Y = UNINSTANTIATED VARIABLE
Z = [a,b,X,d,e|Y]

yes;
X = [X,X]
Y = UNINSTANTIATED VARIABLE
Z = [a,b,X,X,d,e|Y]

yes;
X = [X,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [a,b,X,X,X,d,e|Y]

yes

?- append(X,Y,Z).
X = []
Y = UNINSTANTIATED VARIABLE
Z = UNINSTANTIATED VARIABLE

yes;
X = [X]
Y = UNINSTANTIATED VARIABLE
Z = [X|L3]

yes;
X = [X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X|L3]

yes;
X = [X,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X,X|L3]

yes

?- append(X,[],Z).
X = []
Z = []

yes;
X = [X]
Z = [X]

yes;
X = [X,X]
Z = [X,X]

yes;
X = [X,X,X]
Z = [X,X,X]

yes

?- append(X,[b|c],Z).
X = []
Z = [b|c]

yes;
X = [X]
Z = [X,b|c]

yes;
X = [X,X]
Z = [X,X,b|c]

yes;
X = [X,X,X]
Z = [X,X,X,b|c]

yes

?- append([a,b|X],[d,e|Y],[a|Z]).
X = []
Y = UNINSTANTIATED VARIABLE
Z = [b,d,e|Y]

yes;
X = [X]
Y = UNINSTANTIATED VARIABLE
Z = [b,X,d,e|Y]

yes;
X = [X,X]
Y = UNINSTANTIATED VARIABLE
Z = [b,X,X,d,e|Y]

yes;
X = [X,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [b,X,X,X,d,e|Y]

yes

?- append([a,b|X],[d,e|Y],[a,b|Z]).
X = []
Y = UNINSTANTIATED VARIABLE
Z = [d,e|Y]

yes;
X = [X]
Y = UNINSTANTIATED VARIABLE
Z = [X,d,e|Y]

yes;
X = [X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X,d,e|Y]

yes;
X = [X,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X,X,d,e|Y]

yes

?- append([a,b|X],[d,e|Y],[a,b,c|Z]).
X = [c]
Y = UNINSTANTIATED VARIABLE
Z = [d,e|Y]

yes;
X = [c,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,d,e|Y]

yes;
X = [c,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X,d,e|Y]

yes;
X = [c,X,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X,X,d,e|Y]

yes

?- append([a|X],Y,Z).
X = []
Y = UNINSTANTIATED VARIABLE
Z = [a|L3]

yes;
X = [X]
Y = UNINSTANTIATED VARIABLE
Z = [a,X|L3]

yes;
X = [X,X]
Y = UNINSTANTIATED VARIABLE
Z = [a,X,X|L3]

yes;
X = [X,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [a,X,X,X|L3]

yes

?- append([a|X],[z],Z).
X = []
Z = [a,z]

yes;
X = [X]
Z = [a,X,z]

yes;
X = [X,X]
Z = [a,X,X,z]

yes;
X = [X,X,X]
Z = [a,X,X,X,z]

yes

?- append([a|X],z,Z).
X = []
Z = [a|z]

yes;
X = [X]
Z = [a,X|z]

yes;
X = [X,X]
Z = [a,X,X|z]

yes;
X = [X,X,X]
Z = [a,X,X,X|z]

yes

?- append([a|X],[z|Y],Z).
X = []
Y = UNINSTANTIATED VARIABLE
Z = [a,z|Y]

yes;
X = [X]
Y = UNINSTANTIATED VARIABLE
Z = [a,X,z|Y]

yes;
X = [X,X]
Y = UNINSTANTIATED VARIABLE
Z = [a,X,X,z|Y]

yes;
X = [X,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [a,X,X,X,z|Y]

yes

?- append(a,b,Z).

no

?- append([a],b,c).

no

?- append(a,b,c).

no

?- append([a],b,Z).
Z = [a|b]

yes

?- append(X,Y,c).
X = []
Y = c

yes;

no

?- append([a|X],b,Z).
X = []
Z = [a|b]

yes;
X = [X]
Z = [a,X|b]

yes;
X = [X,X]
Z = [a,X,X|b]

yes;
X = [X,X,X]
Z = [a,X,X,X|b]

yes

?- append([a,b,c|X],z,Z).
X = []
Z = [a,b,c|z]

yes;
X = [X]
Z = [a,b,c,X|z]

yes;
X = [X,X]
Z = [a,b,c,X,X|z]

yes;
X = [X,X,X]
Z = [a,b,c,X,X,X|z]

yes

?- append([a,b,c|X],[z],Z).
X = []
Z = [a,b,c,z]

yes;
X = [X]
Z = [a,b,c,X,z]

yes;
X = [X,X]
Z = [a,b,c,X,X,z]

yes;
X = [X,X,X]
Z = [a,b,c,X,X,X,z]

yes

?- append([a,b,c|X],[],Z).
X = []
Z = [a,b,c]

yes;
X = [X]
Z = [a,b,c,X]

yes;
X = [X,X]
Z = [a,b,c,X,X]

yes;
X = [X,X,X]
Z = [a,b,c,X,X,X]

yes

?- append([a],[a,b,c|X],Z).
X = UNINSTANTIATED VARIABLE
Z = [a,a,b,c|X]

yes

?- append(X,[a,b,c|Y],Z).
X = []
Y = UNINSTANTIATED VARIABLE
Z = [a,b,c|Y]

yes;
X = [X]
Y = UNINSTANTIATED VARIABLE
Z = [X,a,b,c|Y]

yes;
X = [X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X,a,b,c|Y]

yes;
X = [X,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X,X,a,b,c|Y]

yes

?- append(X,z,Z).
X = []
Z = z

yes;
X = [X]
Z = [X|z]

yes;
X = [X,X]
Z = [X,X|z]

yes;
X = [X,X,X]
Z = [X,X,X|z]

yes

?- append(X,Y,[Z|1]).
X = []
Y = [Z|1]
Z = UNINSTANTIATED VARIABLE

yes;
X = [Z]
Y = 1
Z = UNINSTANTIATED VARIABLE

yes;

no

?- append(X,Y,[Z,b|1]).
X = []
Y = [Z,b|1]
Z = UNINSTANTIATED VARIABLE

yes;
X = [Z]
Y = [b|1]
Z = UNINSTANTIATED VARIABLE

yes;
X = [Z,b]
Y = 1
Z = UNINSTANTIATED VARIABLE

yes;

no

?- append(X,Y,[a,b,c,d|1]).
X = []
Y = [a,b,c,d|1]

yes;
X = [a]
Y = [b,c,d|1]

yes;
X = [a,b]
Y = [c,d|1]

yes;
X = [a,b,c]
Y = [d|1]

yes;
X = [a,b,c,d]
Y = 1

yes;

no

?- append(X,Y,[a,b,c,d|Z]).
X = []
Y = [a,b,c,d|Z]
Z = UNINSTANTIATED VARIABLE

yes;
X = [a]
Y = [b,c,d|Z]
Z = UNINSTANTIATED VARIABLE

yes;
X = [a,b]
Y = [c,d|Z]
Z = UNINSTANTIATED VARIABLE

yes;
X = [a,b,c]
Y = [d|Z]
Z = UNINSTANTIATED VARIABLE

yes;
X = [a,b,c,d]
Y = UNINSTANTIATED VARIABLE
Z = UNINSTANTIATED VARIABLE

yes;
X = [a,b,c,d,X]
Y = UNINSTANTIATED VARIABLE
Z = [X|L3]

yes;
X = [a,b,c,d,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X|L3]

yes;
X = [a,b,c,d,X,X,X]
Y = UNINSTANTIATED VARIABLE
Z = [X,X,X|L3]

yes

?- append([a|X],[x,y,z],[a,b,c|Z]).
X = [b,c]
Z = [x,y,z]

yes;
X = [b,c,X]
Z = [X,x,y,z]

yes;
X = [b,c,X,X]
Z = [X,X,x,y,z]

yes;
X = [b,c,X,X,X]
Z = [X,X,X,x,y,z]

yes

?- append([a,b|X],[c,d|X],Y).
X = []
Y = [a,b,c,d]

yes;
X = [X]
Y = [a,b,X,c,d,X]

yes;
X = [X,X]
Y = [a,b,X,X,c,d,X,X]

yes;
X = [X,X,X]
Y = [a,b,X,X,X,c,d,X,X,X]

yes

?- append([a,b|X],[c,d|X],Y), numbervars(Y).
X = []
Y = [a,b,c,d]

yes;
X = [$VAR(0)]
Y = [a,b,$VAR(0),c,d,$VAR(0)]

yes;
X = [$VAR(0),$VAR(1)]
Y = [a,b,$VAR(0),$VAR(1),c,d,$VAR(0),$VAR(1)]

yes;
X = [$VAR(0),$VAR(1),$VAR(2)]
Y = [a,b,$VAR(0),$VAR(1),$VAR(2),c,d,$VAR(0),$VAR(1),$VAR(2)]

yes