prologprolog-toplevel

How to get a numeric value from the consulting file in Prolog?


I am very new to Prolog, and am trying to write a rule that finds a person with more money. I am consulting a file which looks like this:

money('Henry', 200).
money('Sally', 500).
money('Tom', 50).

Here is my attempt at writing a rule that finds the person with more money:

richer(X, Y):- money(X) > money(Y).

Which is not working. I'm kind of lost on how to access that numeric value from the file I'm consulting. Sorry for the simple question, but I have tried Googling it for a while with no success.


Solution

  • Prolog predicates are not functions. There is no money function that "returns" a value. To "get" values out of Prolog predicates you call them with variables as arguments.

    So to get the money a person X has, you write money(X, XMoney). Your predicate could be defined as:

    richer_than(X, Y) :-
        money(X, XMoney),
        money(Y, YMoney),
        XMoney > YMoney.