Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog help with min,max program

Status
Not open for further replies.

lekfr97

Programmer
Jun 14, 2002
2
SE
I have written this Prolog program:

max(X,Y,X) :- X>=Y.
max(X,Y,Y) :- X<Y.
min(X,Y,X) :- X<Y.
min(X,Y,Y) :- X>=Y.
minlist([X],X).
minlist([X,Y|Rest],Min) :- minlist([Y|Rest],MinRest), min(X,MinRest,Min).
maxlist([X],X).
maxlist([X,Y|Rest],Max) :- maxlist([Y|Rest],MaxRest), max(X,MaxRest,Max).

How can I make it work with one command i.e minmax([1,2,3]) giving the answer 1 and 3? This instead of using two as I do now?

Thank you!

/L
 
Hello.
I can't understand what you want to do correctly, but I guess...

*** program ***

minmax(L, Min, Max) :- minlist(L, Min), maxlist(L, Max).

*** test ***

?- minmax([1, 2, 3], Min, Max).
Min = 1
Max = 3 ;
no

?- minmax([9], Min, Max).
Min = 9
Max = 9 ;
no

?- minmax([7, 8, 6], Min, Max).
Min = 6
Max = 8 ;
no
 
Thank you very much. It's very simple now when you know the solution.

Thank you for your help!

/L

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top