I wrote a predicate to find sublists:
sublist([],[]).
sublist([X|T], [X|TS]) :-
sublist(T, TS).
sublist([_|T], X) :-
sublist(T, X).
But it is not correct because it will fail for this:
sublist([1,2,20,4,5,6],[1,2,4,20]).
How to change this predicate to answer true. For that question without making time complexity much bigger?
2015-05-13: Complete rewrite
It’s a bit unclear what you’re after here: Your predicate is called
sublistbut judging from your example query, it seems like you’re after asubset.Anyway, here are definitions for both predicates:
sublist(?L1, +L2)(order and duplicates matter):subset(?L1, +L2)(order and duplicates do not matter):Note that these predicates may exist as built in predicates of your prolog implementation. You should not try to redefine those if this is the case.