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?
another implementation using member is :
member/2 returns true if find the element in a list
Since it seems to be homework I will only give you a few hints:
It seems you are missing the case where an empty list is a sublist of the other one.
You mixed the two cases "the sublist starts here" and "the sublist starts later" into one clause.
It seems the elements of the sublist should be consecutive in the larger list. For that you need two predicates. Essentially you have to remember that the sublist has started when you take apart the lists.
There is no builtin
sublist/2
, only asublist/3
which does something different (filter list with a predicate).If you want
...to succeed, but
...to fail, you might want to consider
Note that if you pass a variable through as Sublist, backtracking will give you a comprehensive set of all possible sublists of List, but this will in general include several repeats of the empty list (because the empty list can combine with all other sublists both ahead and behind of them in an append operation).