Search code examples
listprolog

How to make a sublist in Prolog


How would I do this in prolog?

I have already got this to make a sublist, but now need to make sure the first and last element of the second list are not included in the first list.

sublist(S,L):- append(_,L2,L), append(S,_,L2).

Solution

  • You can use pattern matching such that the head and the tail are non-empty:

    sublist(S,L) :-
        append([_|_],L2,L),
        append(S,[_|_],L2).

    In the first append/3 we thus ensure that the part before the sublist is non-empty, and in the second append/3 we ensure that the part after the sublist is non-empty.