Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog - first list is sublist of second list?

Tags:

prolog

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 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 gives me underfined procedure error.

Is it possible to see how a built-in function is coded? How?

like image 716
user893182 Avatar asked Aug 13 '11 15:08

user893182


1 Answers

sublist( [], _ ).
sublist( [X|XS], [X|XSS] ) :- sublist( XS, XSS ).
sublist( [X|XS], [_|XSS] ) :- sublist( [X|XS], XSS ).
like image 165
ДМИТРИЙ МАЛИКОВ Avatar answered Sep 30 '22 16:09

ДМИТРИЙ МАЛИКОВ