What do these predicates do in Prolog

Viewed 47

I don't understand what exactly these following 4 predicates do:

predicate1([H1,H2|T1],X)  :− 
    append([H1,H2|T1],[H2,H1|T1],X).

predicate2wrapper(In, Out) :- 
    predicate2([ ], In, Out).
predicate2(L, [ ], L).
predicate2(A,[H| T], Out) :- 
    predicate2([H,H|A],T, Out).

predicate3([X,X], [X,X|_]).
predicate3([X,X], [_,H|T]) :− 
    predicate3([X,X],[H|T]).

predicate4([ ], _) .
predicate4([H|T], L) :−
    member(H,L), 
    predicate4(T,L).

I know that all these predicates take lists as their argument(s) and I also tried to run them but I always get a Syntax error: operator expected warning.

Could someone please help me understand what these predicates do and maybe also help me fix this error?

1 Answers

Did you copy this from a pdf or a word-document? The --signs in your :- are not minus signs - this is causing the error. Retype them to solve the issue and then you can analyze the predicates. How to know if chars looking the same are not the same? I would suggest using a diff tool (a program to compare files, as command line or with a gui (winmerge), even available online) or an ascii to hex converter like this.

Example queries which give you a hint what these predicates do:

?- predicate1([1,2,3],L).
?- append([1,2,3],[4,5],L).
?- predicate2wrapper([1,2,3],L).
?- predicate3([c,c],[a,a,b,b]).
?- predicate3([b,b],[a,a,b,b,c,c]).
?- predicate3([X,X],[a,a,c,b,b]).
?- predicate4([a,b],[a,c,b]).
?- predicate4([a,d],[a,c,b]).
?- member(a,[a,c,b]).
?- member(d,[a,c,b]).
Related