tneduts
tneduts

Reputation: 115

Prolog - member rule using concat

Can anyone explain how this member rule works using concatenation? I figured concat just returns a new list rather than seeing if a element is in the list. Concat rule is taken from my textbook.

 concat([ ], L, L). 
 concat([H|T], L, [H|M]) :- concat(T, L, M). 

 member(X, L) :- concat(L1, [X | L2], L).

 ex) member(a, [a, b, c]) => True

Upvotes: 2

Views: 132

Answers (1)

Steven
Steven

Reputation: 2477

Prolog predicates usually work in different ways. Concat can be used to create a list, but it can also be used to split one list into two. One way to read the call to concat in this case is

Are there lists L1 and [X|L2] such that their concatenation is L?

Because X is the head of the second list, you know that if this statement is true, then X is a member of L.

In the example, L1 would unify with [] (i.e. all elements before a) an L2 would unify with [b, c] (all elements after a).

Upvotes: 2

Related Questions