删除序言列表中的值和该值的所有重复项 [英] Remove both the value and all duplicates of that value in a list in prolog
本文介绍了删除序言列表中的值和该值的所有重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在从序言列表中删除值时遇到一些麻烦. 我有一个颜色列表,我想为其添加颜色列表,并保留所有没有重复的值,然后删除其余的值.
I'm having some trouble removing values from a list in prolog. I have a list of colors and I want to add a list of colors to it and keep all the values that have no duplicate and remove the rest.
[green, red, blue, purple, yellow, brown, orange, black, purple]
所以紫色在此列表中出现了两次,我想将它们都删除. 这是我要返回的列表.
so purple appears twice in this list and I want to remove both of them. This is the list I want to be returned.
[green, red, blue, yellow, brown, orange, black]
我目前具有删除所有重复项的功能,但是我无法消除两种紫色.
I currently have this to remove all the duplicates but I can't get both purples out.
mymember(X,[H|_]) :- X==H,!.
mymember(X,[_|T]) :- mymember(X,T).
not(A) :- \+ call(A).
set([],[]).
set([Head|Tail],[Head|Out]) :-
not(mymember(Head,Tail)),
set(Tail, Out).
set([Head|Tail],Out) :-
mymember(Head,Tail),
set(Tail, Out).
这是我现在得到的结果:
this is the result I get now:
[green, red, blue, yellow, brown, orange, black, purple]
推荐答案
简单方法...单线:
singletons(Xs,Zs) :-
findall( X , ( append(P,[X|S],Xs), \+member(X,P), \+member(X,S) ) , Zs )
.
这篇关于删除序言列表中的值和该值的所有重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文