反转没有重复项的列表(将其转换为集合)->序言

发布于 2025-01-10 15:57:47 字数 531 浏览 0 评论 0原文

我需要从列表中删除所有重复项,然后以相反的顺序显示它。

到目前为止我有这个:

reverseInSet([], Y,R):-
 R=Y.

reverseInSet([H|T], Y, R):- 
 removeElement(H,T,T1), 
 reverseInSet(T1, [H|Y], R).


removeElement(_,[],[]).    

removeElement(X,[X|T],L):-    
 removeElement(X,T,L).

removeElement(X,[Y|T],L):-   
 removeElement(X,T,L1),
 L=[Y|L1].

输出是这样的:

reverseInSet([1,2,3,3,9,3],[],P).
P = [9, 3, 2, 1]
P = [3, 9, 3, 2, 1]
P = [9, 3, 3, 2, 1]
P = [9, 3, 3, 2, 1]
P = [3, 9, 3, 3, 2, 1]

有什么想法吗?

I need to delete all duplicates from a list and then show it in reverse order.

So far I have this:

reverseInSet([], Y,R):-
 R=Y.

reverseInSet([H|T], Y, R):- 
 removeElement(H,T,T1), 
 reverseInSet(T1, [H|Y], R).


removeElement(_,[],[]).    

removeElement(X,[X|T],L):-    
 removeElement(X,T,L).

removeElement(X,[Y|T],L):-   
 removeElement(X,T,L1),
 L=[Y|L1].

And the output is this:

reverseInSet([1,2,3,3,9,3],[],P).
P = [9, 3, 2, 1]
P = [3, 9, 3, 2, 1]
P = [9, 3, 3, 2, 1]
P = [9, 3, 3, 2, 1]
P = [3, 9, 3, 3, 2, 1]

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冷弦 2025-01-17 15:57:47

这是最简单的方法。如果源列表中存在重复条目,则保留最后一个此类条目,并丢弃其前面的条目。

list_to_set_reverse( Xs, Ys ) :- list_to_set( Xs, Zs ) , reverse(Zs,Ys) .

list_to_set( []     , []     ) .
list_to_set( [X|Xs] , Ys     ) :- member(X,Xs), !, list_to_set(Xs,Ys) .
list_to_set( [X|Xs] , [X|Ys] ) :-                  list_to_set(Xs,Ys) .

您也可以一次完成。您只需要使用累加器以相反的顺序构建结果列表:

list_to_set_reverse( Xs, Ys ) :- list_to_set( Xs, [], Ys ) .

list_to_set( []     , Ys , Ys ) .
list_to_set( [X|Xs] , Ts , Ys ) :- member(X,Xs), !, list_to_set(Xs,Ts,Ys) .
list_to_set( [X|Xs] , Ts , Ys ) :-                  list_to_set(Xs,[X|Ts],Ys) .

这也会保留源列表中的最后一个重复项。但是,由于我们在这里使用累加器来构建结果列表,因此很容易重新调整它以保留任何重复项中的第一个:我们只需检查候选项是否已在结果集中(而不是而不是检查它是否稍后出现在源列表中)。

list_to_set_reverse( Xs, Ys ) :- list_to_set( Xs, [], Ys ) .

list_to_set( []     , Ys , Ys ) .
list_to_set( [X|Xs] , Ts , Ys ) :- member(X,Ts), !, list_to_set(Xs,Ts,Ys).
list_to_set( [X|Xs] , Ts , Ys ) :-                  list_to_set(Xs,[X|Ts],Ys) .

This is about the simplest way to do it. In the case of duplicate entries in the source list, the last such entry is kept and the ones preceding it are discarded.

list_to_set_reverse( Xs, Ys ) :- list_to_set( Xs, Zs ) , reverse(Zs,Ys) .

list_to_set( []     , []     ) .
list_to_set( [X|Xs] , Ys     ) :- member(X,Xs), !, list_to_set(Xs,Ys) .
list_to_set( [X|Xs] , [X|Ys] ) :-                  list_to_set(Xs,Ys) .

You could also do it in a single pass, too. You just need to use an accumulator to build the result list in reverse order:

list_to_set_reverse( Xs, Ys ) :- list_to_set( Xs, [], Ys ) .

list_to_set( []     , Ys , Ys ) .
list_to_set( [X|Xs] , Ts , Ys ) :- member(X,Xs), !, list_to_set(Xs,Ts,Ys) .
list_to_set( [X|Xs] , Ts , Ys ) :-                  list_to_set(Xs,[X|Ts],Ys) .

This also keeps the last duplicates in the source list. But, since we're using an accumulator here to build the result list, it's easy enough to rejigger it to keep the first of any duplicate items: we just check to see if a candidate item is already in the result set or not (rather than check to see if it occurs later in the source list).

list_to_set_reverse( Xs, Ys ) :- list_to_set( Xs, [], Ys ) .

list_to_set( []     , Ys , Ys ) .
list_to_set( [X|Xs] , Ts , Ys ) :- member(X,Ts), !, list_to_set(Xs,Ts,Ys).
list_to_set( [X|Xs] , Ts , Ys ) :-                  list_to_set(Xs,[X|Ts],Ys) .

萌无敌 2025-01-17 15:57:47

使用 reif 库,既纯粹又合理确定:

:- use_module(library(reif)).

reverse_without_duplicates(Lst, LstRevDeDup) :-
    reverse_without_duplicates_(Lst, [], LstRevDeDup).


reverse_without_duplicates_([], Lst, Lst).

reverse_without_duplicates_([H|T], Upto, LstRevDeDup) :-
    memberd_t(H, T, Bool),
    reverse_without_duplicates_bool_(Bool, T, H, Upto, LstRevDeDup).

reverse_without_duplicates_bool_(true, T, _H, Upto, LstRevDeDup) :-
    reverse_without_duplicates_(T, Upto, LstRevDeDup).
reverse_without_duplicates_bool_(false, T, H, Upto, LstRevDeDup) :-
    % Include head
    reverse_without_duplicates_(T, [H|Upto], LstRevDeDup).

结果swi-prolog:

?- time(reverse_without_duplicates([1, 2, 3, 3, 9, 3], X)).
% 52 inferences, 0.000 CPU in 0.000 seconds (88% CPU, 1025277 Lips)
X = [3,9,2,1].

这种纯粹的方法显示了合理的答案:

?- time(reverse_without_duplicates([1, 2, N, 3, 9, 3], L)).
% 49 inferences, 0.000 CPU in 0.000 seconds (87% CPU, 1030603 Lips)
N = 1,
L = [3,9,1,2] ;
% 127 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 845646 Lips)
N = 2,
L = [3,9,2,1] ;
% 152 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1051910 Lips)
N = 3,
L = [3,9,2,1] ;
% 178 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1229859 Lips)
N = 9,
L = [3,9,2,1] ;
% 88 inferences, 0.000 CPU in 0.000 seconds (97% CPU, 648633 Lips)
L = [3,9,N,2,1],
dif(N,1),
dif(N,9),
dif(N,3),
dif(N,2).

Using reif library, to be both pure and reasonably deterministic:

:- use_module(library(reif)).

reverse_without_duplicates(Lst, LstRevDeDup) :-
    reverse_without_duplicates_(Lst, [], LstRevDeDup).


reverse_without_duplicates_([], Lst, Lst).

reverse_without_duplicates_([H|T], Upto, LstRevDeDup) :-
    memberd_t(H, T, Bool),
    reverse_without_duplicates_bool_(Bool, T, H, Upto, LstRevDeDup).

reverse_without_duplicates_bool_(true, T, _H, Upto, LstRevDeDup) :-
    reverse_without_duplicates_(T, Upto, LstRevDeDup).
reverse_without_duplicates_bool_(false, T, H, Upto, LstRevDeDup) :-
    % Include head
    reverse_without_duplicates_(T, [H|Upto], LstRevDeDup).

Result in swi-prolog:

?- time(reverse_without_duplicates([1, 2, 3, 3, 9, 3], X)).
% 52 inferences, 0.000 CPU in 0.000 seconds (88% CPU, 1025277 Lips)
X = [3,9,2,1].

This pure method shows sensible answers with:

?- time(reverse_without_duplicates([1, 2, N, 3, 9, 3], L)).
% 49 inferences, 0.000 CPU in 0.000 seconds (87% CPU, 1030603 Lips)
N = 1,
L = [3,9,1,2] ;
% 127 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 845646 Lips)
N = 2,
L = [3,9,2,1] ;
% 152 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1051910 Lips)
N = 3,
L = [3,9,2,1] ;
% 178 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1229859 Lips)
N = 9,
L = [3,9,2,1] ;
% 88 inferences, 0.000 CPU in 0.000 seconds (97% CPU, 648633 Lips)
L = [3,9,N,2,1],
dif(N,1),
dif(N,9),
dif(N,3),
dif(N,2).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文