0

My goal is to find the maximum value, or more in general an arithmetic expression, for some objects.

Here's an example:

card(one).
card(two).
card(three).

value(one,10).
value(two,20).
value(three,30).

It's obvious that the "winner" is the card 3. But I don't know how to build a predicate that is able to compare each value of an object with the others.

EDIT: I've tried this one

winner(X):- card(X), value(X, ValueX), card(Y), value(Y, ValueY), ValueX <   ValueY.

The result is the "winners", but I need the "absolute winner" which is only one. In the example above we have card 2 and card 3, because card 2 win on 1 and 3 win on 2. So we have 2 winners, but the absolute one is only card 3. Is there a way to achieve this?

user840718
  • 1,563
  • 6
  • 29
  • 54
  • 1
    Here are some ideas to build upon. You should run the Prolog interpreter and enter your facts that you have in. Then try the following queries: `card(X).` (you should see three results: `X = one`, `X = two`, and `X = three`). The try, `card(X), value(X, ValueX), card(Y), value(Y, ValueY), ValueX < ValueY.` (note the commas). This will find all the cards `X` and `Y` such that the value of card `X` is less than the value of card `Y`. – lurker Oct 19 '15 at 01:02
  • Well, this is a good starting point. Anyway is not a general purpose solution, because if I have not 2 but 10000 objects I need to write a lot of code. Maybye I can use a list, but I know that list works only for numbers in prolog and not for predicates. Any further suggestions? – user840718 Oct 19 '15 at 10:09
  • 1
    I know it's not a general solution. I was showing you concepts so that at least you could try something yourself and ask more specific question when you get stuck. Stackoverflow is not intended a site where you post you assignment and people do all your work for you. Perhaps you could show what you've tried? – lurker Oct 19 '15 at 10:23
  • Actually, the code in the first comment by @lurker is general enough, and almost solves your exact problem. And since your comment suggests to me that you have not really put almost any effort in learning Prolog, I would suggest trying a basic tutorial, or even just searching a bit more diligently here on Stack Overflow. –  Oct 19 '15 at 10:41
  • I tried a recursive version adding winner(X) in the formula above, but the program never end. Maybe I'm not skilled enough, but if in prolog could be a solution to my problem, it's fine. – user840718 Oct 19 '15 at 11:00
  • You need to show what you've attempted, then perhaps others here could help you with a specific problem with what you've tried. – lurker Oct 19 '15 at 11:12
  • Here's what I did: winner(X):- card(X), value(X, ValueX), card(Y), value(Y, ValueY), ValueX>ValueY, winner(X). I'm interested in the "absolute" winner, not all winners. This formula do not end, anyway without the last predicate the formula gives me all the winners and the last one, is the only one I'm looking for. Thank you for the patience. – user840718 Oct 19 '15 at 11:46
  • Please do not enter code in comments. It's hard to read. Please edit your question and add your code there, properly formatted. – lurker Oct 19 '15 at 14:15
  • Ok, thank you. I did it. – user840718 Oct 19 '15 at 19:47

0 Answers0