在 Prolog Amzi 中,_(下划线)在这种情况下意味着什么
我一直在尝试学习一些Prolog Amzi。这是一个示例问题,我无法完全理解这个问题。
这是代码。
/* Facts */
parent(mary,tom).
parent(john,tom).
parent(mary,alice).
parent(john,alice).
sex(mary, female).
sex(john, male).
sex(tom, male).
sex(alice, female).
/* Rules */
mother(X):-
sex(X,female),
parent(X,_).
father(X):-
sex(X, male),
parent(X,_).
sibling(X,Y):-
parent(M,X),
sex(M,female),
parent(F,X),
sex(F,male),
parent(M,Y),
parent(F,Y).
sibling1(X,Y):-
parent(M,X),
sex(M,female),
parent(F,X),
sex(F,male),
parent(M,Y),
parent(F,Y),
X \= Y.
go:-
nl, nl,
write('Hello there .....'), nl,
write('Testing on Prolog2.'), nl,
write('End Job'), n1.
该问题询问规则 mother(X) 和father(X) 中下划线的功能是什么,并附有输出示例。
我似乎不明白下划线在这种情况下的含义以及示例输出的含义。
I have been trying to learn some Prolog Amzi. This is an example question and can't quite wrap my head around this question.
This is the code.
/* Facts */
parent(mary,tom).
parent(john,tom).
parent(mary,alice).
parent(john,alice).
sex(mary, female).
sex(john, male).
sex(tom, male).
sex(alice, female).
/* Rules */
mother(X):-
sex(X,female),
parent(X,_).
father(X):-
sex(X, male),
parent(X,_).
sibling(X,Y):-
parent(M,X),
sex(M,female),
parent(F,X),
sex(F,male),
parent(M,Y),
parent(F,Y).
sibling1(X,Y):-
parent(M,X),
sex(M,female),
parent(F,X),
sex(F,male),
parent(M,Y),
parent(F,Y),
X \= Y.
go:-
nl, nl,
write('Hello there .....'), nl,
write('Testing on Prolog2.'), nl,
write('End Job'), n1.
The question asks what the function of the underscore in the rules mother(X) and father(X) is with examples of output.
I don't seem to understand what the underscore means in this context and what it means by example output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

在任何上下文中,匿名变量(用
_
表示)表示其具体值不相关的参数。例如,规则
mother(X) :- sex(X, Female),parent(X, _).
指出X是母亲,X 必须是女性,并且也是某人的父母(其姓名不相关,必须由匿名变量表示) 。匿名变量 (
_
) 不绑定到值,并且多次出现并不意味着值相等。例如,考虑以下事实:
要查明玛丽的孩子是谁,您可以询问:
要查明玛丽是否是某人的父母,您可以询问:
要查明是否有人是别人的父母,您可以询问:
要查明是否有人是自己的父母,您可以询问:
In any context, an anonymous variable (denoted by
_
) represents an argument whose specific value is irrelevant.For example, the rule
mother(X) :- sex(X, female), parent(X, _).
states that for X to be a mother, X must be female and also be the parent of someone (whose name is irrelevant and must represented by an anonymous variable).The anonymous variable (
_
) does not bind to values, and multiple occurrences of it do not imply equal values.Consider, for example, the following facts:
To find out who Mary's children are, you can ask:
To find out whether Mary is someone's parent, you can ask:
To find out whether there is someone who is someone else's parent, you can ask:
To find out whether there are people who are their own parents, you can ask: