Feb 20, 2002 #1 5143LS Technical User Feb 20, 2002 2 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
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
Feb 26, 2002 #2 mizt Programmer Feb 26, 2002 2 GB 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 Upvote 0 Downvote
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
Apr 11, 2002 #4 Guest_imported New member Jan 1, 1970 0 i search an games: the naval battle realizaed in prolog Upvote 0 Downvote
Apr 11, 2002 #5 pimi Programmer Mar 18, 2002 36 JP 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 Upvote 0 Downvote
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
Apr 11, 2002 #6 pimi Programmer Mar 18, 2002 36 JP 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. Upvote 0 Downvote
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.