Sergio Calderon
Sergio Calderon

Reputation: 867

Print a value from a fact in Prolog?

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?

Upvotes: 0

Views: 807

Answers (2)

Shevliaskovic
Shevliaskovic

Reputation: 1564

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

Upvotes: 0

m09
m09

Reputation: 7493

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.

Upvotes: 3

Related Questions