如何找到在序言中列出的差异,并确定该列表是相同的(相同的元素,但不必须具有相同的顺序)(How to

2019-10-20 07:37发布

  1. 如果我有两个列表说A和B由不同字母的UPP。 并[b,A,C]在表A和表B I怎么能得到序言给我C,其是不包括在列表中乙实施例A有元素的列表并[b,d,C,E] 。 因此,如果i型difference([b,a,c],[b,d,c,e],C). 我想要的答案为: C=[a]

  2. 如果我有两个列表,其中列表A由抬离[A,B,C]和列表B是[C,A,B] i的要得到的结果是Yes ,如果i型: same([a,b,c],[c,a,b]). 因为它们含有相同的元素的事实,他们是在一个不同的顺序并不重要。 此外,它应该只回答是,如果所有的元素都是相同的,如果没有3/4是正确的。

Answer 1:

因为这看起来像功课我会给一些想法不仅没有实际的代码:

首先,您可以用确定的元素是一个列表的成员成员的程序开始:

  member(X, [X|_]).
  member(X, [_|T]) :- member(X, T).

因此,X是列表的头部或尾部的一员。

然后,您的第一个问题都可以回答如下:

  1. If list A is empty then list C is empty
  2. The head of A (HA) is the head of C if member(HA, B) is false AND the Tail of C (CT) can be found by recursively calling the procdure with the tail of A (TA), B and CT.
  3. Otherwise, if HA is a member of B then just recusively call the procedure on TA, B and C

类似地,第二个问题可以使用上述程序来回答。 如果字母A中的名单,是不是在B是空的,A的每个元素是B的成员,那么他们是相同的。

希望这会有帮助。 您可以随时发布你尝试过,所以我们可以给更多的指针是什么。



文章来源: How to find differences in lists in prolog and determine if the lists are the same (same elements but doesnt have to have the same order)
标签: prolog