Prolog - Step Siblings 的链式规则 [英] Prolog - Chain rule for Step Siblings

查看:87
本文介绍了Prolog - Step Siblings 的链式规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Prolog 的新手,有一个关于为共享共同父母"的继兄弟姐妹编写链式规则"的问题.

I am a newbie to Prolog, and have a question regarding programming a "chain rule" for step-siblings that share a "common parent".

在我的程序中,我假设存在断言 X 是 Y 的父级的 parent(X,Y) 事实.

In my program, I am assuming that the existence of the parent(X,Y) fact that asserts that X is a parent of Y.

我需要一个规则chain(X,Y,L):如果X是Y的祖先,那么L是列表包含 X、Y 和 Y 的所有祖先(也是 X 的后代),按年龄降序排列(最老的).换句话说,我的列表应该包含所有将一个人与祖先联系起来的人.

I need a rule chain(X,Y,L): if X is an ancestor of Y, then L is the list containing X, Y and all ancestors of Y who are also descendants of X, listed in descending order of age (oldest first). In other words, my list should contain all the people that link a person with an ancestor.

例如:如果 chain(peter,mary,[peter,paul,sue,mary]),则 peterpaulpaulsue 的父级,suema​​ry 的父级>.

Eg: If chain(peter,mary,[peter,paul,sue,mary]), then peter is the parent of paul, paul is the parent of sue, and sue is the parent of mary.

注意:我熟悉stepSibling(a,b)关系,他们的关系通过父母partner(X,Y)确定强>;其中兄弟姐妹ab是他们各自父母的孩子,通过child(a,X)和<强>孩子(b,Y).因此;我只是对两个继兄弟姐妹共享一个共同的父母的关系感到困惑.即.子关系可能如下所示:child(a,X)child(b,X).

Note: I am familiar with the stepSibling(a,b) relationship where their relationship is qualified via their parents partner(X,Y); where siblings a and b are children of their respective parents via the relationship child(a,X) and child(b,Y). Hence; I am only confused with the relationship where both stepsiblings share a common parent. ie. A child relationship that may look like this: child(a,X) and child(b,X).

推荐答案

对于我们在早期 Prolog 课程中讨论的常见谱系问题,这是一种有趣的转折.让我们首先考虑一个简化,如果 X 是 Y 的祖先,则 ancestor(X,Y) 为真.

This is kind of an interesting twist on the usual genealogy problems we discuss in early Prolog courses. Let's consider a simplification first, ancestor(X,Y) is true if X is an ancestor of Y.

所以你有一个谓词 parent(X,Y) 表示 X 是 Y 的父级.你可以这样写 ancestor/2:

So you have a predicate parent(X,Y) which says X is a parent of Y. You can write ancestor/2 like this:

% base case: your parent is an ancestor
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).

有了这样的示例数据库,它应该可以正常工作:

With a sample database like this it should work fine:

parent(peter, paul).
parent(paul, sue).
parent(sue, mary).

这适用于像 ancestor(peter, mary) 这样的查询,它与您想要的很接近.下一步是保留链,它看起来像这样:

This will work for a query like ancestor(peter, mary), which is close to what you want. The next step would be to retain the chain, which would look something like this:

chain(X, Y, [X,Y])     :- parent(X, Y).
chain(X, Z, [X|Chain]) :- parent(X, Y), chain(Y, Z, Chain).

这似乎有效:

?- chain(peter, mary, X).
X = [peter, paul, sue, mary] ;
false.

不过我很担心,因为你的问题提到了继兄弟姐妹,而且连锁店应该包括其他人.这只是箔条还是您有其他要求?如果是这样,它们不会反映在您的示例中,因此我可能需要您用其他详细信息来补充您的问题.

I worry though, because your question mentions step siblings and that the chain should include other people. Is that just chaff or do you have additional requirements? If so, they're not reflected in your example, so I may need you to augment your question with additional details.

这篇关于Prolog - Step Siblings 的链式规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆