List 在prolog中管理列表
我是Prolog新手,正在寻求帮助。我想做的是得到一个列表,它由在给定列表中至少重复两次的元素组成 范例 L'=[1,2,1,3,4,3,2]=>L=[1,2,3] 到目前为止,我能够计算每个连续变量的发生率List 在prolog中管理列表,list,prolog,filtering,List,Prolog,Filtering,我是Prolog新手,正在寻求帮助。我想做的是得到一个列表,它由在给定列表中至少重复两次的元素组成 范例 L'=[1,2,1,3,4,3,2]=>L=[1,2,3] 到目前为止,我能够计算每个连续变量的发生率 % pack(L1,L2) :- the list L2 is obtained from the list L1 by packing % repeated occurrences of elements into separate sublists. % (list,lis
% pack(L1,L2) :- the list L2 is obtained from the list L1 by packing
% repeated occurrences of elements into separate sublists.
% (list,list) (+,?)
pack([],[]).
pack([X|Xs],[Z|Zs]) :- transfer(X,Xs,Ys,Z), pack(Ys,Zs).
% transfer(X,Xs,Ys,Z) Ys is the list that remains from the list Xs
% when all leading copies of X are removed and transfered to Z
transfer(X,[],[],[X]).
transfer(X,[Y|Ys],[Y|Ys],[X]) :- X \= Y.
transfer(X,[X|Xs],Ys,[X|Zs]) :- transfer(X,Xs,Ys,Zs).
% encode(L1,L2) :- the list L2 is obtained from the list L1 by run-length
% encoding. Consecutive duplicates of elements are encoded as terms [N,E],
% where N is the number of duplicates of the element E.
% (list,list) (+,?)
encode(L1,L2) :- pack(L1,L), transform(L,L2).
transform([],[]).
transform([[X|Xs]|Ys],[[N,X]|Zs]) :- length([X|Xs],N), transform(Ys,Zs).
这将返回以下Touple列表
?- encode([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
X = [[4,a],[1,b],[2,c],[2,a],[1,d][4,e]]
但构建一个包含重复至少两次的不同元素的列表仍然存在问题
如果有人能帮我,或者给我指出大致的方向,那就太好了
提前感谢您可以编写一个过程:
% E it's the list of are elements from L that repeat at least twice
elements_that_repeat_at_least_twice(L, E) :-
elements_that_repeat_at_least_twice(L, [], E).
elements_that_repeat_at_least_twice([H|Ls], Dupl, E) :-
...
在重复至少两次的元素中,添加的列表Dupl将使您验证的每个元素保持多次。使用[H | Ls]检查L的每个元素。
使用memberchk/2验证H是否在L中:那么它至少是重复的。如果它还没有在Dupl中,添加到它,然后递归。请记住编写递归基本情况(在空列表[]处停止)
现在我看到您添加了一些代码:然后我完成了建议:
elements_that_repeat_at_least_twice([], Dupl, Dupl).
elements_that_repeat_at_least_twice([H|Ls], Dupl, E) :-
( memberchk(H, Ls)
-> ( \+ memberchk(H, Dupl)
-> Dupl1 = [H|Dupl]
; Dupl1 = Dupl
)
; Dupl1 = Dupl
),
elements_that_repeat_at_least_twice(Ls, Dupl1, E).
完成后,请记住反转重复列表
an element E of list L should:
be a member of list L',
be a member of list L'' where L'' is list L' if we remove element E.
检查,和/或