I have a list of people that contains list of people that contains name+age.
person(bob, 20).
person(charlie, 15).
person(steve, 21).
person(david, 25).
I want to create a predicate oldest(List, Name) that will take a list of names and return a name of the oldest person in this list. Something like this:
?- oldest([bob,charlie,steve,david], Name).
Name = david.
?- oldest([bob,charlie], Name).
Name = bob.
As I understand - it should iterate through the list and compare age of everyone in it, but the problem for me is that you should get age first and then return a name.
How can it be done? Thank you in advance.
This is all I have right now:
%If person is older
maximum([X|Y], MaxName, MaxAge):-
person(X, Age),
Age > MaxAge,
%Set MaxName to X, do not really understand how to do it
maximum(Y,MaxName,Age).
%Skipping a younger person, passing current MaxName and MaxValue to the next element
maximum([X|Y], MaxName, MaxAge):-
person(X, Age),
Age =< MaxAge,
maximum(Y,MaxName,MaxAge).