listrecursionprolog

Prolog - first list is sublist of second list?


For example:

isin([1,2,3], [1,0,1,2,3,0])

will yield true because 123 is inside of 101230

I wrote the following code:

isin([AH|AT],[AH|AT]).

isin([AH|AT],[BH|BT]):- AH = BH, isin(AT,BT), isin([AH|AT],BT).

seems not working. Try not to use any built-in functions and BTW, Prolog has a built-in sublist(L1, L2) function.

How do I write a query against a built-in function using SWI-Prolog ? I tried to directly write:

?- sublist([1],[2]).

but it returns an undefined procedure error.

Also, is it possible to see how a built-in function is coded and how ?


Solution

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