从列表中删除不重复的项目
我需要做一个练习,我必须消除列表中不重复的元素,之前我做了一个练习来消除列表中重复的元素。这是我的代码,用于消除列表中重复的元素,但我不知道如何修改它以生成新代码来消除列表中不重复的元素。有人可以帮助我吗?请。
simp([H,H|T],X):-!, simp([H|T],X).
simp([H|T],[H|X]):-simp(T,X).
I need to do an exercise where I must eliminate the elements of a list that are NOT duplicated, previously I made one to eliminate the elements of a list that ARE duplicated. This is my code to eliminate the elements that ARE duplicated in a list but I don't know how to modify it to generate a new code to eliminate the elements of a list that are NOT duplicated. Can somebody help me? Please.
simp([H,H|T],X):-!, simp([H|T],X).
simp([H|T],[H|X]):-simp(T,X).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 reif 库,既纯粹又合理确定(类似答案):
swi-prolog 的结果:
Using reif library, to be both pure and reasonably deterministic (similar answer):
Result in swi-prolog:
首先你的问题有歧义,你说的是
这应该意味着删除唯一的元素。
例如,
但是,在后面的评论中,您还说
这不是“删除唯一元素”,而是“将重复元素作为“集合”获取,但保持原始顺序”。
尽管如此,我仍然会给你两个解决方案:
定义
dup
来生成所有重复元素定义
remove_unique_elems
定义
duplicate_elements
First of all, your question is ambiguous, you said that
It should mean to remove the unique elements.
For example,
However, in the comment later, you also said that
That is not "remove the unique elements", but "get the duplicate elements as a 'set', but keep the original order".
Nevertheless, I will still give you two solutions:
Define
dup
to generate all duplicate elementsDefine
remove_unique_elems
Define
duplicate_elements