Search code examples
prologunification

Member predicate


When you call member(Item, List) with an uninstanciated list, Prolog unifies and returns a list containing item. I want a rule that returns true/false and does not try to unify. Is there such a rule?


Solution

  • I would use a guard, like

    is_member(E, L) :- nonvar(L), memberchk(E, L).
    

    memberchk/2 it's a deterministic version of member/2, to be used to find if the list contains at least 1 occurrence of element. Cannot act as a generator, but it's more efficient. The guard is required anyway.