Prolog: "\+ memberchk" inside "setof"

37 Views Asked by At

I have some Prolog code of the following form:

setof(Element,some_function(P,Element,C),Elements)

such that all instantiations of term Element in solution to some_function are collected in list Elements.

I now want to improve this with memberchk such that any instantiation of term Element is only added to Elements if it doesn't exist on a list List.

I have tried the following:

setof(Element,some_function(P,Element,C,List),Elements)

................

some_function(P,Element,C,List) :-
.... the function as it was ....,
\+ memberchk(Element,List).

This does work in the sense that only elements that are not on List are added. But the problem is that setof now fails when all instantiations of term Element are already on List. Whereas I would like it to return an empty list Elements, but not fail.

Any ideas on how to do this? Thanks!

0

There are 0 best solutions below