2015-10-15 60 views
0

我学习Prolog的,并具有以下给出行:什么值应该代替“下划线”?

Consider the following Prolog program. It contains facts regarding instructors of classes and 
in which classes students are enrolled. instructor(p,c) means that professor p is the instructor 
of course c. enrolled(s, c) means that student s is enrolled in course c. We want to use these 
facts to answer queries concerning the professors who teach particular students. 

    instructor(fibonacci, math100). 
    instructor(turing, cs330). 
    instructor(galileo, phys210). 
    enrolled(john, math100). 
    enrolled(sofia, cs330). 
    enrolled(ryan, phys210). 
    enrolled(lisa, math100). 
    enrolled(matt, cs330). 
    enrolled(lisa, cs330). 

然后还有一个问题:

What would Prolog return given the following queries? If a query has more than one answer, list all the answers. 

?- instructor(galileo, _). 
?- instructor(_, ee100). 

我已经完成了所有的其他问题,但在遇到问题时与理解'_'。 Prolog会产生什么答案?对于?- instructor(galileo, _).我的假设是phys210但第二个呢?

回答

1

'会发生什么'的常见答案是'试一试'。 _是prolog的“不关心”,它将匿名与任何东西统一。就这样,你的第一个查询:

?- instructor(galileo, _). 

会成功,与true。与此相比,查询:

?- instructor(galileo, X). 

哪些成功,的确结合Xphys210

你的第二个目标失败,因为没有子句instructor其中第二个参数是ee100