开发者

Two clause definition to find the maximum number on a list

开发者 https://www.devze.com 2022-12-12 15:52 出处:网络
How would I write a two clause recursive definition to find the maximum value in a list.So far I have written this:

How would I write a two clause recursive definition to find the maximum value in a list. So far I have written this:

 max(L,M):-  

 max([H|T],M):-

max(T,H,M).
max([],M,M).
max([H|T],Y,M):-
   H =< Y,
   max(T,Y,M).
max([开发者_开发技巧H|T],Y,M):-
   H > Y,
   max(T,H,M).

This doesn't work, it says there is a syntax error which I can't quite see, and I know it isn't two clause either. Anyone know how I could simplify it to make it two clause?


As you, I use the 'max' name for the predicate. This implementation don't rely in any built-in predicate:

max([X],X).
max([X|Xs],X):- max(Xs,Y), X >=Y.
max([X|Xs],N):- max(Xs,N), N > X.


The syntax error results from the fact that the first two clauses have no body.

To answer your question, observe that the maximum of a list can be defined inductively as follows:

  • The maximum of a list with one element is that element.
  • The maximum of a list with multiple elements is the largest of the head and the maximum of the tail.

Thus,

max_list([H], H).
max_list([H|T], M2) :- 
  max_list(T, M),
  M2 is max(H, M).

This code uses max/2 (SWI-Prolog, GNU-Prolog). Note that most or all Prolog implementations will have a built-in function max_list/2 (S, G), so there is actually no need to define it yourself.

Edit: Bakore notes that a tail recursive implementation may be more efficient. You can do this by defining a predicate max_list/3 which takes an additional argument C, namely the largest value seen so far.

max_list([H|T], M) :- max_list(T, H, M). 

max_list([], C, C).
max_list([H|T], C, M) :- C2 is max(C, H), max_list(T, C2, M).


Here is a solution for max in lists of lists

max_list([], C, C).
max_list([H|T], C, M) :- C2 is max(C, H), max_list(T, C2, M).

max_list([], []).
max_list([[H|HB]|B],[RH|RB]) :- max_list(HB, H, RH), max_list(B, RB).

ex: max_list([[1,3,6], [6,3,8,2],[2,1,0]]).


DOMAINS

num=INTEGER
list = num*

PREDICATES

nondeterm maxList(list,num)

CLAUSES

maxList([A],A).   
maxList([A|List],Max):- Max=A,maxList(List,Max1),A>=Max1.  
maxList([A|List],Max):- Max=Max1,
maxList(List,Max1),A< Max1.

GOAL

maxList([1,2,3,5,4],Max).


This one works for sure

l:-listing.
m(L,X):-aku2(L,0,X).
aku2([],B,B).
aku2([G|O],Maks,C):-maks(Maks,G,Maks1),aku2(O,Maks1,C).
maks(A,B,C):-A>B, C is A.
maks(A,B,C):-A=<B, C is B.


I think the code below will solve the problem:

max_list([],0).
max_list([H],H).
max_list([H|T],M):- max_list(T,M1),M is max(H,M1).


I know this question is old, but here's an answer using the if-then-else construct:

maxmember([X],X).
maxmember([H|T],Max) :- maxmember(T, M),(H>M -> Max is H ; Max is M).


max(0,[]).

max(R,[H|T]):-

max(Y,T),
H > Y,
R is H.

max(R,[H|T]):-

max(Y,T),
H =< Y,
R is Y.


list([H],H).
list([H1,H2|T],X):-H1>H2,list([H1|T],X).
list([_|T],X):-list(T,X).

Goal: list([3,9,4,5],M)

Output: M=9

0

精彩评论

暂无评论...
验证码 换一张
取 消