I’m reading Ivan Bratko’s Prolog Programming for Artificial Intelligence book and I have no experience with Prolog before. In book a sublist relation for a list is formulated as:
S is a sublist of L if:
1) L can be decomposed into two lists, L1 and L2, and
2) L2 can be decomposed into two lists, S and some L3.
And relation is given as:
sublist(S, L) :-
conc(L1, L2, L),
conc(S, L3, L2).
conc([], L, L).
conc([X|L1], L2, [X|L3]) :-
conc(L1, L2, L3).
It seems odd to me why we don’t just decompose list into two lists and check whether one of the lists matches with S?
3
Answers
This should help; the key word/concept is
difference list
.From “Prolog Techniques” by Attila Csenki, Chapter 2 (Free PDF with embedded ads)
A little latter in the book is this image
Second part of book is actually a separate book,
“Applications of Prolog” by Attila Csenki (Free PDF with embedded ads)
For comparison purposes, the definition of the
sublist/2
predicate used in the Logtalk library is:IIRC, this is a common definition.Some sample calls could be:
Update
Noticed that the definition in the question and the one in my answer don’t have the same semantics. The definition in the question implies consecutive elements. E.g.
One issue in this definition is that the empty list solution is generated multiple times.
I get the impression that you’re very close when you write:
Just decompose the list
L
into three lists and you’re there:L1
is a list that precedes the sublist (“prefix”),S
is the sublist itself, andL3
is a list that follows the sublist (“suffix”).As
conc/3
can only concatenate (or decompose) two lists—not three—a conjunction of twoconc/3
goals is required.conc(L1,L2,L), conc(S,L3,L2)
expresses above relation.