Implement the member predicate as a one-liner

Viewed 40801

Interview question!

This is how you normally define the member relation in Prolog:

member(X, [X|_]).        % member(X, [Head|Tail]) is true if X = Head 
                         % that is, if X is the head of the list
member(X, [_|Tail]) :-   % or if X is a member of Tail,
  member(X, Tail).       % ie. if member(X, Tail) is true.

Define it using only one rule.

4 Answers

You can also try this:

member(X,L) :- append(_,[X|_],L).
Related