Print a value from a fact in Prolog?

断了今生、忘了曾经 提交于 2019-12-20 07:25:26

问题


Let's say I have these facts:

champion(Real_Madrid).
second_place(Atl).

How do I print the "Real_Madrid" string value from a query in Prolog so I can say for example:

Champions(Something).

Real_Madrid

Any way to do that?


回答1:


In Prolog, atoms need quotes if they start by an upper-case letter so that they're not confused with variables.

Here, you could write:

champion('Real Madrid').
second_place('Atl').

Then the simple query:

?- champion(Something).

would print the required bindings:

Something = 'Real Madrid'.

If needed, you can find more information about Prolog syntax here.




回答2:


You will need a variable that will print that value for you.

In Prolog, variables start with upper-case.

So if you execute any query like:

champion(X). or champion(Something). or champion(Winner). etc, the result would be the same and it would be X=Real Madrid or Something=Real Madrid or Winner=Real Madrid etc.

The name of the variable could be anything. Even 'Loser' and the result would be the same.

Don't forget the period (.) at the end of the query you execute



来源:https://stackoverflow.com/questions/23644500/print-a-value-from-a-fact-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!