从列表中删除不重复的项目

发布于 2025-01-14 16:35:06 字数 202 浏览 1 评论 0原文

我需要做一个练习,我必须消除列表中不重复的元素,之前我做了一个练习来消除列表中重复的元素。这是我的代码,用于消除列表中重复的元素,但我不知道如何修改它以生成新代码来消除列表中不重复的元素。有人可以帮助我吗?请。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

允世 2025-01-21 16:35:06

使用 reif 库,既纯粹又合理确定(类似答案):

:- use_module(library(reif)).

duplicate_elements(LstFull, LstDuplicates) :-
    duplicate_elements_(LstFull, [], LstDuplicatesRev),
    reverse(LstDuplicatesRev, LstDuplicates).

duplicate_elements_([], L, L).
% U means LstUpto
duplicate_elements_([H|T], U, LstD) :-
    memberd_t(H, T, Bool),
    (Bool == true -> duplicate_elements_add_(U, H, U1) ; U1 = U),
    duplicate_elements_(T, U1, LstD).

duplicate_elements_add_(U, E, U1) :-
    % Prevent adding a duplicate to U1 more than once
    (memberchk(E, U) -> U1 = U ; U1 = [E|U]).

swi-prolog 的结果:

?- time(duplicate_elements([a,b,1,2,c,A,2,1,4], D)).
% 105 inferences, 0.000 CPU in 0.000 seconds (92% CPU, 1494726 Lips)
A = a,
D = [a,1,2] ;
% 177 inferences, 0.000 CPU in 0.000 seconds (94% CPU, 1261726 Lips)
A = b,
D = [b,1,2] ;
% 193 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1288643 Lips)
A = 1,
D = [1,2] ;
% 214 inferences, 0.000 CPU in 0.000 seconds (96% CPU, 1349996 Lips)
A = 2,
D = [1,2] ;
% 237 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1515152 Lips)
A = c,
D = [1,2,c] ;
% 360 inferences, 0.000 CPU in 0.000 seconds (99% CPU, 1892217 Lips)
A = 4,
D = [1,2,4] ;
% 49 inferences, 0.000 CPU in 0.000 seconds (95% CPU, 575563 Lips)
D = [1,2],
dif(A,a),
dif(A,4),
dif(A,c),
dif(A,2),
dif(A,1),
dif(A,b).

Using reif library, to be both pure and reasonably deterministic (similar answer):

:- use_module(library(reif)).

duplicate_elements(LstFull, LstDuplicates) :-
    duplicate_elements_(LstFull, [], LstDuplicatesRev),
    reverse(LstDuplicatesRev, LstDuplicates).

duplicate_elements_([], L, L).
% U means LstUpto
duplicate_elements_([H|T], U, LstD) :-
    memberd_t(H, T, Bool),
    (Bool == true -> duplicate_elements_add_(U, H, U1) ; U1 = U),
    duplicate_elements_(T, U1, LstD).

duplicate_elements_add_(U, E, U1) :-
    % Prevent adding a duplicate to U1 more than once
    (memberchk(E, U) -> U1 = U ; U1 = [E|U]).

Result in swi-prolog:

?- time(duplicate_elements([a,b,1,2,c,A,2,1,4], D)).
% 105 inferences, 0.000 CPU in 0.000 seconds (92% CPU, 1494726 Lips)
A = a,
D = [a,1,2] ;
% 177 inferences, 0.000 CPU in 0.000 seconds (94% CPU, 1261726 Lips)
A = b,
D = [b,1,2] ;
% 193 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1288643 Lips)
A = 1,
D = [1,2] ;
% 214 inferences, 0.000 CPU in 0.000 seconds (96% CPU, 1349996 Lips)
A = 2,
D = [1,2] ;
% 237 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1515152 Lips)
A = c,
D = [1,2,c] ;
% 360 inferences, 0.000 CPU in 0.000 seconds (99% CPU, 1892217 Lips)
A = 4,
D = [1,2,4] ;
% 49 inferences, 0.000 CPU in 0.000 seconds (95% CPU, 575563 Lips)
D = [1,2],
dif(A,a),
dif(A,4),
dif(A,c),
dif(A,2),
dif(A,1),
dif(A,b).
北城挽邺 2025-01-21 16:35:06

首先你的问题有歧义,你说的是

消除列表中不重复的元素

这应该意味着删除唯一的元素。

例如,

?- remove_unique_elems([a,b,1,2,c,a,2,1,4], R). 
R = [a,1,2,a,2,1].

但是,在后面的评论中,您还说

如果我写?- Elimination([a,b,1,2,c,a,2,1,4], R)。。我想要得到R = [a,1,2]。

这不是“删除唯一元素”,而是“将重复元素作为“集合”获取,但保持原始顺序”。

尽管如此,我仍然会给你两个解决方案:

  1. 定义dup来生成所有重复元素

    删除(X,[X|L],L) :- !.
    删除(Y,[X | Xs],[X | Xt]):-删除(Y,Xs,Xt)。
    
    dup(X,L):-成员(X,L),删除(X,L,L2),memberchk(X,L2)。
    
    ?- dup(X,[a,b,1,2,c,a,2,1,4])。
    X = a;
    X = 1 ;
    X = 2 ;
    X = a;
    X = 2 ;
    X = 1 ;
    错误的。;
    
    ?- dup(X,[1,2,3,4])。
    错误的。
    
  2. 定义remove_unique_elems

    remove_unique_elems(L,R) :-
      ( bagof(X, dup(X,L), Xs)
      -> R = Xs
      ; R = []
      )。
    
    ?-remove_unique_elems([a,b,1,2,c,a,2,1,4],R)。
    R = [a,1,2,a,2,1]。
    
    ?-remove_unique_elems([1,2,3,4],R)。
    R = []。
    
    ?-remove_unique_elems([a,a],[a,a])。
    真的。
    
  3. 定义duplicate_elements

    remove_dup(DL,NDL) :- 反向(DL,DLR),remove_dup_(DLR,NDLR),反向(NDLR,NDL)。
    remove_dup_([], []) :- !.
    删除_dup_([X | Xs],L):-成员(X,Xs),!,删除_dup_(Xs,L)。
    删除_dup_([X | Xs],[X | Ys]):-删除_dup_(Xs,Ys)。
    
    重复元素(L,R):-
      ( bagof(X, dup(X,L), Xs)
      ->删除_dup(Xs,R)
      ; R = []
      )。
    
    ?-重复元素([a,b,1,2,c,a,2,1,4],R)。
    R = [a,1,2]。
    
    ?-重复元素([1,2,3,4],R)。
    R = []。
    
    ?-重复元素([a,a],[a,a])。
    错误的。
    

First of all, your question is ambiguous, you said that

eliminate the elements of a list that are NOT duplicated

It should mean to remove the unique elements.

For example,

?- remove_unique_elems([a,b,1,2,c,a,2,1,4], R). 
R = [a,1,2,a,2,1].

However, in the comment later, you also said that

If I write ?- eliminate([a,b,1,2,c,a,2,1,4], R).. I wan to get R = [a,1,2].

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:

  1. Define dup to generate all duplicate elements

    delete(X,[X|L],L) :- !.
    delete(Y,[X|Xs],[X|Xt]) :- delete(Y,Xs,Xt).
    
    dup(X,L) :- member(X,L), delete(X,L,L2), memberchk(X,L2).
    
    ?- dup(X,[a,b,1,2,c,a,2,1,4]).
    X = a ;
    X = 1 ;
    X = 2 ;
    X = a ;
    X = 2 ;
    X = 1 ;
    false.;
    
    ?- dup(X,[1,2,3,4]).
    false.
    
  2. Define remove_unique_elems

    remove_unique_elems(L,R) :-
      (   bagof(X, dup(X,L), Xs)
      ->  R = Xs
      ;   R = []
      ).
    
    ?- remove_unique_elems([a,b,1,2,c,a,2,1,4], R).
    R = [a,1,2,a,2,1].
    
    ?- remove_unique_elems([1,2,3,4], R).
    R = [].
    
    ?- remove_unique_elems([a,a],[a,a]).
    true.
    
  3. Define duplicate_elements

    remove_dup(DL,NDL) :- reverse(DL, DLR), remove_dup_(DLR, NDLR),reverse(NDLR, NDL).
    remove_dup_([], []) :- !.
    remove_dup_([X|Xs], L) :- member(X, Xs), !, remove_dup_(Xs, L).
    remove_dup_([X|Xs], [X|Ys]) :- remove_dup_(Xs, Ys).
    
    duplicate_elements(L,R) :-
      (   bagof(X, dup(X,L), Xs)
      ->  remove_dup(Xs, R)
      ;   R = []
      ).
    
    ?- duplicate_elements([a,b,1,2,c,a,2,1,4], R).
    R = [a,1,2].
    
    ?- duplicate_elements([1,2,3,4], R).
    R = [].
    
    ?- duplicate_elements([a,a], [a,a]).
    false.
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文