1
votes

I am trying to create a predicate in prolog called isDuped( Y ) that only succeeds if Y is a list of even length and each element in the list appears twice in a row (i.e. [1,1,2,2,3,3,4,4]). What I currently have is:

isDuped( Y ) :-
    Y == [].
isDuped( Y ) :-
    [ A, B | C ] = Y,
    A == B,
    isDuped( C ).

However, one of my professor's unit tests is supposed to return true, but as I have it written it returns false. isDuped([1,_]) is supposed to return true, but I have no idea what I need to change. Any help would be appreciated.

2

2 Answers

1
votes

Why not

isDuped( [] ).
isDuped([A, A | C] ) :-
    isDuped( C ).
0
votes

In your code, comparison A == B checks if the terms A and B are identical. An uninstantiated variable and a number are not identical terms.

What your professor seems to want (not clear from the problem formulation you gave, but clear from the test) is unification =: just change A == B to A = B.