0
votes

I've got an assignment where I have to define a predicate named two(X,L), which is true if the list L contains at least two occurrences of the element X, using only the append predicate. The right-hand side of any of my rules may use only append, or use the predicate two(X,L). So for example, my desired output would look like this.

two(x,[a,a,b,a,c,b,a]).
X = a
X = b

two(c,[a,a,b,a,c,b,a]).
FALSE

I've been trying to think of a way to do this, but based off of my knowledge of append the only predicate we could really use within two(X,L) is append(X,X,L). Append isn't a true/false thing is it, it just tries to put X within L twice.

I don't need someone to solve the assignment for me, I just need a stepping stone because I'm confused as to how to accomplish this task only using append.

2

2 Answers

1
votes
two(X,List):-append(_,[X|Rest],List),append(_,[X|_Rest2],Rest).
0
votes

Based on the comments and the previous answer of @user27815, an alternative solution could look like the following code, in which the helper predicate needs to be integrated into the the two predicate:

two(X, [Y|Ys]) :-
    two_helper(X, [Y|Ys]),
    \+ two_helper(X, Ys).

two(X, [_|Ys]) :-
    two(X, Ys).

two_helper(X,List) :- append(_,[X|Rest],List),append(_,[X|_Rest2],Rest).