In SWI-Prolog I want to establish the list L from two lists L1 and L2 with the smallest count of elements under the condition, that 1 ∈ L1
and 1 ∈ L2
.
If 1 ∉ L1
and 1 ∈ L2
, then L = L1
. If 1 ∈ L1
and 1 ∉ L2
, then L = L2
. If 1 ∉ L1
and 1 ∉ L2
, then the predicate returns false.
I could evaluate this in Prolog with the following conditions:
minset_one(D1, D2, T) :- ((member(1, D1), not(member(1, D2))) -> T=D1).
minset_one(D1, D2, T) :- ((not(member(1, D1)), member(1, D2)) -> T=D2).
minset_one(D1, D2, T) :- (member(1, D1), member(1, D2), length(D1,L1), length(D2,L2), L1 >= L2) -> T=D2.
minset_one(D1, D2, T) :- (member(1, D1), member(1, D2), length(D1,L1), length(D2,L2), L2 > L1) -> T=D1.
My problem with that function is, the member function is called very often. Is their a way to reduce the complexity of that predicate in that way, the functions
member(1, D1)
member(1, D2)
length(D1, L1)
length(D2, L2)
are called only one time?