Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
合并两个列表而不重复prolog_Prolog - Fatal编程技术网

合并两个列表而不重复prolog

合并两个列表而不重复prolog,prolog,Prolog,我正在写一个序言作业,要求我把两个列表合并在一起,但不要重复。例如组合([a,b,c],[c,d,e],Result)。将给出结果=[a,b,c,d,e]。。但是我得到的是Result=[a,b,c,c,d,e]。相反 我的代码如下所示: combine([Head|Tail], Lst2, [Head|Result]) :- combine(Tail, Lst2, Result). merge( [] , [] , [] ). % the merger

我正在写一个序言作业,要求我把两个列表合并在一起,但不要重复。例如
组合([a,b,c],[c,d,e],Result)。
将给出
结果=[a,b,c,d,e]。
。但是我得到的是
Result=[a,b,c,c,d,e]。
相反

我的代码如下所示:

combine([Head|Tail], Lst2, [Head|Result]) :- 
    combine(Tail, Lst2, Result).
merge( []     , []     , []     ).   % the merger of two empty lists is the empty list.
merge( [X|Xs] , []     , [X|Xs] ).   % the merger of a non-empty list and an empty list is the non-empty list
merge( []     , [Y|Ys] , [Y|Ys] ).   % the merger of an empty list and a non-empty list is the non-empty list
merge( [X|Xs] , [Y|Ys] , [X|Zs] ) :- % to merge two non-empty lists
  X @< Y,                            % and the left item is less than the right item
  merge( Xs, [Y|Ys], Zs )            % take the left item and recurse down
  .                                  %
merge( [X|Xs] , [Y|Ys] , [Y|Zs] ) :- % to merge two non-empty lists
  X @> Y,                            % and the left item is greater than the right item
  merge( [X|Xs], Ys, Zs )            % take the right item and recurse down
  .                                  %
merge( [X|Xs] , [Y|Ys] , [X|Zs] ) :- % to merge two non-empty lists
  X == Y,                            % and the left and right items are equal
  merge( Xs, Ys, Zs )                % take either item, discard the other and recurse down
  .                                  % Easy!

我如何才能在同一规则中删除重复项,而不使用帮助函数?

假设每个源列表构成一个有序集(例如,它是有序的和唯一的),您可以简单地合并它们,并在遇到重复项时丢弃它们。大概是这样的:

combine([Head|Tail], Lst2, [Head|Result]) :- 
    combine(Tail, Lst2, Result).
merge( []     , []     , []     ).   % the merger of two empty lists is the empty list.
merge( [X|Xs] , []     , [X|Xs] ).   % the merger of a non-empty list and an empty list is the non-empty list
merge( []     , [Y|Ys] , [Y|Ys] ).   % the merger of an empty list and a non-empty list is the non-empty list
merge( [X|Xs] , [Y|Ys] , [X|Zs] ) :- % to merge two non-empty lists
  X @< Y,                            % and the left item is less than the right item
  merge( Xs, [Y|Ys], Zs )            % take the left item and recurse down
  .                                  %
merge( [X|Xs] , [Y|Ys] , [Y|Zs] ) :- % to merge two non-empty lists
  X @> Y,                            % and the left item is greater than the right item
  merge( [X|Xs], Ys, Zs )            % take the right item and recurse down
  .                                  %
merge( [X|Xs] , [Y|Ys] , [X|Zs] ) :- % to merge two non-empty lists
  X == Y,                            % and the left and right items are equal
  merge( Xs, Ys, Zs )                % take either item, discard the other and recurse down
  .                                  % Easy!
合并([]、[]、[])。%两个空列表的合并就是空列表。
合并([X|Xs],[X|Xs])。%非空列表和空列表的合并就是非空列表
合并([],[Y|Ys],[Y|Ys])。%空列表和非空列表的合并就是非空列表
合并([X|Xs],[Y|Ys],[X|Zs]):-%合并两个非空列表
X@Y%,左侧项目大于右侧项目
合并([X | Xs],Ys,Zs)%n获取正确的项并向下递归
.                                  %
合并([X|Xs],[Y|Ys],[X|Zs]):-%合并两个非空列表
X==Y%,左右项相等
合并(Xs、Ys、Zs)%取其中一项,丢弃另一项并向下递归
.                                  % 容易的!
如果要保留重复项,只需修改
=
条件的最后一个子句,即可将
X
Y
推到合并列表中。

尝试使用member

combine(List1,List2,CombinedList):-
combine1(List1,List2,List), %Return List with items not in List2
append(List,List2,AppendedList), %Join List and List2
sort(AppendedList,CombinedList). %Sort the final List

combine1([],_,[]).
combine1([H|T],List2,[H|L]):-
    \+member(H,List2),
    combine1(T,List2,L).
combine1([H|T],List2,L):-
    member(H,List2),
    combine1(T,List2,L).
示例:

?- combine([a,b,d],[c],CombinedList).
CombinedList = [a, b, c, d]
false

?- combine([a,d,e],[c,b],CombinedList).
CombinedList = [a, b, c, d, e]
false

?- combine([],[c,b,a],CombinedList).
CombinedList = [a, b, c]

?-combine([a,b,c],[c,d,e],CombinedList).
CombinedList = [a, b, c, d, e]
false

这两份名单都排好了吗?你能假设列表本身不包含重复项吗?是的,它们是有序的,我认为列表本身不会包含任何重复项。反例:merge([],[2,3],X)。你的第二条和第三条限制太多了。@tiffi:修正了。谢谢