Prolog的功课帮助

时间:2010-03-05 15:34:34

标签: list prolog

好的,我的最后一个序言问题。这是常见的基因学问题。

我想要拿一个事实列表并有一个叫做后代的函数     将返回包含所有后代的列表。例如:

根据规则:

parent('Bob', 'Tim').
parent('Joe', 'Bob').

函数调用:

    descendant('Joe', X).

应该返回:

        X = ['Bob', 'Tim'].

我可以让它返回'Joe'的直系后代而不是整行。这就是我所拥有的。

 % Recursive case
  descendant(X,DList) :- parent(X,A), NewDList = [A|DList], 
                         descendant(A, NewDList).
 % Base case, I have a feeling this is wrong.
  descendant(_,[]).

此代码似乎只返回true或false,或只是一个空的[]。

我可以在我可能需要查看的内容上使用一些帮助。感谢。

3 个答案:

答案 0 :(得分:1)

首先,我们将创建一个可以找到单个后代的谓词。

descendant(X, Y) :- parent(X, Y).
descendant(X, Y) :- parent(X, Z), descendant(Z, Y).

然后我们可以使用findall谓词列出所有后代:

descendants(X, L) :- findall(A, descendant(X, A), L).

所以,例如:

parent(bob, tim).
parent(joe, bob).
parent(joe, mary).

descendant(X, Y) :- parent(X, Y).
descendant(X, Y) :- parent(X, Z), descendant(Z, Y).

descendants(X, L) :- findall(A, descendant(X, A), L).

给出:

?- descendants(joe, X).
X = [bob, mary, tim].

答案 1 :(得分:0)

我的Prolog有点生疏,我不愿意回答这个问题 - 你不会那么学习。

我只是指出你不应该在那里有那个赋值语句 - NewDList = [A | DList] - 这在Prolog编程风格中被认为是不好的形式 - 赋值只应该在没有的情况下使用一个“纯粹的”逻辑解决方案 - 当然不是这种情况。

干杯, 克雷格。

答案 2 :(得分:0)

parent('Bob', 'Tim').
parent('Joe', 'Bob').

descendant(X,[H|T]) :- parent(X,H), descendant(H, T).
descendant(X,[]) .

返回

?- descendant('Joe', L).
L = ['Bob', 'Tim'] ;
L = ['Bob'] ;
L = [].

实际上很难编写仅返回['Bob', 'Tim']的谓词,因为列表['Bob']也是有效的。如果你决定只留下最长的链条,那就太复杂了

如果我理解错误,这里有一个版本:

desc(X, A) :- parent(X,H), desc(H, A).
desc(X, A) :- X = A.
相关问题