问题
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