1

What is the easiest way to find who is the tallest in Prolog:

height(lisa,1.65).
height(sam,1.70).
height(luke,1.92).
height(nicole,1.54).

I want to write

tallest(X) :- Y is bigger than other Y's
newtothis
  • 35
  • 4

3 Answers3

5

SWI-Prolog has some different ways to solve this problem, for instance by means of library(solution_sequences)

?- order_by([desc(H)],height(P,H)).
H = 1.92,
P = luke ;
...

or using library(aggregate):

?- aggregate(max(H,P),height(P,H),max(_,P)).
P = luke.

less sophisticate Prologs probably will offer setof/3 and last/2:

?- setof(H:P,height(P,H),L),last(L,_:P).
P = luke,
L = [1.54:nicole, 1.65:lisa, 1.7:sam, 1.92:luke].

and still more basic engines, lacking setof/3, will offer

?- height(P,H),\+((height(_,L),L>H)).
P = luke,
H = 1.92 ;
CapelliC
  • 59,646
  • 5
  • 47
  • 90
3

Supposing that tallest(X) succeeds if, and only if, person X is taller than all other persons, I think that a correct answer would be:

tallest(X) :-
    height(X, H),
    forall((height(Y, H1),
            X \= Y),
            H > H1), !.

First scenario:

height(lisa,1.65).
height(sam,1.70).
height(luke,1.92).
height(nicole,1.54).
?- tallest(X).
X = luke.

Second scenario:

height(lisa,   1.65).
height(sam,    1.70).
height(luke,   1.92).
height(nicole, 1.54).
height(bob,    1.92). % Bob is as tall as Luke!
?- tallest(X).
false.
slago
  • 5,025
  • 2
  • 10
  • 23
1
height(lisa,1.65).
height(sam,1.70).
height(luke,1.92).
height(nicole,1.54).

max_height(Person, Height, [[Person, Height]]).
max_height(P , H , [[P, H]|Tail]) :- max_height(_ , H2, Tail), H  > H2.
max_height(P2, H2, [[_, H]|Tail]) :- max_height(P2, H2, Tail), H =< H2.

tallest(X) :- findall([P, H], height(P, H), Bag), max_height(X, _, Bag).

There are ways to avoid writing max_height : Prolog, find minimum in a list

MWB
  • 11,740
  • 6
  • 46
  • 91