简体   繁体   中英

Prolog List predicate

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
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM