Prolog adding and removing list element if non present in second list
Posted
by logically
on Stack Overflow
See other posts from Stack Overflow
or by logically
Published on 2010-06-07T10:03:45Z
Indexed on
2010/06/07
10:22 UTC
Read the original article
Hit count: 367
I don't know what I'm missing here.
I wan't to add an element if it is in arg1 but not in arg2 and want to remove an element if it is in arg1 but not in arg2.
I'm using an if condition with includes function that return true if the element is in the arg2 list, false otherwise. Then use built it predicates append and select to add or remove.
I'm getting false to all my objectives searches. I comment and uncomment depending on what predicate I want, add or remove.
includes([],_).
includes([P|Z],S) :- memberchk(P,S), includes(Z,S).
addop([],list,res).
addop([P|R],list,res) :- includes(P,s0) -> addop(R,list,res) ; append(P,list,res), addop(R,list,res).
rem([],list,res).
rem([P|R],list,res) :- includes(P,list) -> rem(R,list,res) ; select(P,list,res),rem(R,list,res).
Thanks for help.
© Stack Overflow or respective owner