0

Suppose I had an atom name and age pair:

person(bob,42).
person(jeff,12).
person(adam,23).
...

And I wanted to find through a query (not predicate) the youngest person.

How would I make such a query?

Jebathon
  • 4,310
  • 14
  • 57
  • 108
  • See [this question and answers](http://stackoverflow.com/questions/27317069/collect-all-minimum-solutions-from-a-backtrackable-predicate). By the way, what is the difference between a query and a predicate? If you just take the body of a predicate, it would be the query, I guess? –  Nov 07 '15 at 08:52

2 Answers2

1

You can formulate a multi-part query with a universal quantifier, like this:

person(X,AgeX), forall(person(Y,AgeY), X=Y;AgeX<AgeY).

Essentially, this query says that X must be such that for each known person Y in the database it should either be the same person (X=Y part) or the other person must be older (AgeX<AgeY part)

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

library(aggregate) solves such task, and much more... try

?- aggregate(min(Age,Person), person(Person,Age), R).
R = min(12, jeff).
CapelliC
  • 59,646
  • 5
  • 47
  • 90