Define sublist(Xs, Ys)
:
this holds when Xs
is a list containing some of the elements of Ys
, in the same order they appear in the list Ys
. For example, sublist(X,[a,b,c])
should have the eight solutions X=[]; X=[c]; X=[b]; X=[b,c]; X=[a]; X=[a,c]; X=[a,b];
and X=[a,b,c].
My solution is like this:
sublist([],[]).
sublist([],[_|_]).
sublist([X|Xs],[Y|Ys]):- (
X=Y->sublist(Xs,Ys);
sublist([X|Xs],Ys)
).
However, it only outputs:
X = [] ;
X = [a] ;
X = [a, b] ;
X = [a, b, c].
what's wrong with my solution?
X=Y
will always succeed when either variable is uninstantiated). – Tomas By