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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog help

Status
Not open for further replies.

5143LS

Technical User
Joined
Feb 20, 2002
Messages
2
Location
SE
Hi!

Want help with some Prolog assignments:

1. Find the maximum and minimum in a list of numbers.

2. Find out if a given predicate is a member of a list, ex. member(X,L) checks if X is part of list L.

How do I start? Any ideas?

Thank you

/Clark
 
thats pretty easy..

Something is the member of a list if it is the Head of the list or if it is in the tail of the list..

so

member(X, [X,Head]).

member(X, [Head,Tail]):- member(X, Tail).

Hope that helps..

Euan
 
member(X, [X,Head]).
member(X, [Head,Tail]):- member(X, Tail).

?- member(a, [c, b, a]).
no

Please replace some "," with "|".

member(X, [X|Head]).
member(X, [Head|Tail]) :- member(X, Tail).

?- member(a, [c, b, a]).
yes
 
To find the maximum in a list of numbers...

max([N1|Ns], Max) :-
maxsub(Ns, N1, Max).

maxsub([], MaxTmp, MaxTmp) :-
!.
maxsub([N1|Ns], MaxTmp, Max) :-
N1 > MaxTmp, !, maxsub(Ns, N1, Max).
maxsub([N1|Ns], MaxTmp, Max) :-
maxsub(Ns, MaxTmp, Max).

This pattern can be used also for minimum number.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top