列表列表的序言成员 [英] Prolog member of List of List
本文介绍了列表列表的序言成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想定义一个谓词p(X),其中X是列表的列表. p(X)是真的,如果X中只有一个元素Y,则X和Y没有公共元素.
I want to define a predicat p(X), where X is list of lists. p(X) is true, if in X there is only one element Y, that X and Y have no common elements.
这不是家庭作业.这是我考试的一个示例问题.谢谢.
This is not homework. This is a example problem for my exam. Thank you.
推荐答案
写下来:
p(X):- %// "define a predicate p(X), where X is list of lists, such that
%// p(X) is true if in X there is only one element Y,
% findall( Y, (member(Y,X), ........ ), [_])
%// such that X and Y have no common elements".
findall( Y, (member(Y,X), \+ have_common_element(X,Y) ), [_]).
have_common_element(A,B):- member(X,A), memberchk(X,B).
现在
8 ?- p([[1,[2]],[2],[3]]). %// both [2] and [3] have no common elts w/ X
false.
9 ?- p([[1,[2]],[2]]). %// [1,[2]] has one common elt w/ X, viz. [2]
true.
序言列表是异构的.元素也可以是列表.还有它的元素.
Prolog lists are heterogeneous. An element might be a list as well. And its element too.
这篇关于列表列表的序言成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文