0
votes

Pretty new to PROLOG, and i'm having a problem with this list.

I need a predicate (sublist(X,Y) that is true if list X is a sublist of list Y. The sublist is the original list, in the same order, yet some elements may have been removed. For instance, some sample user input is:

?- sublist([a,b],[a,e,b,d,s,e]).
Yes

?- sublist([a,b],[a,e,e,f]).
No

?- sublist([a,b],[b,a]).
No

?- sublist(X,[a,b,c]).
X = [] ;
X = [a] ;
X = [a, b] ;
X = [a, b, c] ;
X = [a, c] ;
X = [b] ;
X = [b, c] ;
X = [c] ;
No
1

1 Answers

0
votes
sublist( [], _ ).
sublist( [X|XS], [X|XSS] ) :- sublist( XS, XSS ).
sublist( [X|XS], [_|XSS] ) :- sublist( [X|XS], XSS ).

Got it! This works for both sub sequences and non sub-sequences in the list