Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
List 序言列出了不同之处_List_Prolog_Symmetric Difference - Fatal编程技术网

List 序言列出了不同之处

List 序言列出了不同之处,list,prolog,symmetric-difference,List,Prolog,Symmetric Difference,我正在尝试用prolog编写一个程序,它将执行以下操作: diffSet([a,b,c,d], [a,b,e,f], X). X = [c,d,e,f] 我写道: diffSet([], _, []). diffSet([H|T1],Set,Z):- member(Set, H), !, diffSet(T1,Set,Z). diffSet([H|T], Set, [H|Set2]):- diffSet(T,Set,Set2). 但这样我只能从第一个列表中获取元素。如何从第二个元素中提取元素

我正在尝试用prolog编写一个程序,它将执行以下操作:

diffSet([a,b,c,d], [a,b,e,f], X).
X = [c,d,e,f]
我写道:

diffSet([], _, []).
diffSet([H|T1],Set,Z):- member(Set, H), !, diffSet(T1,Set,Z).
diffSet([H|T], Set, [H|Set2]):- diffSet(T,Set,Set2).
但这样我只能从第一个列表中获取元素。如何从第二个元素中提取元素

@编辑: 成员正在检查H是否在集合中

member([H|_], H).
member([_|T], H):- member(T, H).
存在从列表中删除元素的方法:

diffSet([], X, X).

diffSet([H|T1],Set,Z):-
 member(H, Set),       % NOTE: arguments swapped!
 !, delete(T1, H, T2), % avoid duplicates in first list
 delete(Set, H, Set2), % remove duplicates in second list
 diffSet(T2, Set2, Z).

diffSet([H|T], Set, [H|Set2]) :-
 diffSet(T,Set,Set2).
存在从列表中删除元素的方法:

diffSet([], X, X).

diffSet([H|T1],Set,Z):-
 member(H, Set),       % NOTE: arguments swapped!
 !, delete(T1, H, T2), % avoid duplicates in first list
 delete(Set, H, Set2), % remove duplicates in second list
 diffSet(T2, Set2, Z).

diffSet([H|T], Set, [H|Set2]) :-
 diffSet(T,Set,Set2).

故意避免@chac提到的内置功能,这是一种不雅观的工作方式

notcommon([], _, []).

notcommon([H1|T1], L2, [H1|Diffs]) :-
    not(member(H1, L2)),
    notcommon(T1, L2, Diffs).

notcommon([_|T1], L2, Diffs) :-
    notcommon(T1, L2, Diffs).

alldiffs(L1, L2, AllDiffs) :-
    notcommon(L1, L2, SetOne),
    notcommon(L2, L1, SetTwo),
    append(SetOne, SetTwo, AllDiffs).


    ? alldiffs([a,b,c,d], [a,b,e,f], X).
    X = [c, d, e, f] .

故意避免@chac提到的内置功能,这是一种不雅观的工作方式

notcommon([], _, []).

notcommon([H1|T1], L2, [H1|Diffs]) :-
    not(member(H1, L2)),
    notcommon(T1, L2, Diffs).

notcommon([_|T1], L2, Diffs) :-
    notcommon(T1, L2, Diffs).

alldiffs(L1, L2, AllDiffs) :-
    notcommon(L1, L2, SetOne),
    notcommon(L2, L1, SetTwo),
    append(SetOne, SetTwo, AllDiffs).


    ? alldiffs([a,b,c,d], [a,b,e,f], X).
    X = [c, d, e, f] .

或者只使用内置的。如果您只想完成工作:

notcommon(L1, L2, Result) :-

    intersection(L1, L2, Intersec),
    append(L1, L2, AllItems),
    subtract(AllItems, Intersec, Result).

    ?- notcommon([a,b,c,d], [a,b,e,f], X).
    X = [c, d, e, f].

或者只使用内置的。如果您只想完成工作:

notcommon(L1, L2, Result) :-

    intersection(L1, L2, Intersec),
    append(L1, L2, AllItems),
    subtract(AllItems, Intersec, Result).

    ?- notcommon([a,b,c,d], [a,b,e,f], X).
    X = [c, d, e, f].

为什么要从第二个列表中删除重复项,而不是从第一个列表中删除?为什么要从第二个列表中删除重复项,而不是从第一个列表中删除重复项?