/* Tutorial 10
   CSC324
   Fall 2005
*/

/** find a maximum in a list allmax( +List, -Index, -Max)
(use setof to find all ;-))

Do NOT use ! or ->

*/
allmax( [X], 0, X) :- !.
allmax( [H|L], N, X) :-
       allmax( L, SN, SX),
       (
         H > SX % IF H > SX
       ->
         % THEN
         X = H,
         N = 0
      ;
         % ELSE
         (
           H = SX,
           X = H,
           N = 0
         ;
           X = SX,
           N is SN+1
         )
       ).

/** find a maximum in a list allmax( +List, -Index, -Max)
(use setof to find all ;-))

You may use ! or ->

*/
allmax2( [X], 0, X).
allmax2( [H|L], N, X) :-
        L \= [],
        allmax2( L, SN, SX),
        (
          H > SX,
          X = H,
          N = 0
        ;
          (
            H = SX,
            X = H,
            N = 0
          ;
            H =< SX,
            X = SX,
            N is SN+1
          )
        ).

/* ---------- TEST CASES ----------
    RUN WITH BOTH allmax/3 and allmax2/3

?- allmax( [1,2,3,4,-3,4,2,1,4,0,4], Index, Max).

Index = 3
Max = 4 ;

Index = 5
Max = 4 ;

Index = 3
Max = 4 ;

Index = 8
Max = 4 ;

Index = 3
Max = 4 ;

Index = 5
Max = 4 ;

Index = 3
Max = 4 ;

Index = 10
Max = 4 ;

No
?- setof( (I, X), allmax( [1,2,3,4,-3,4,2,1,4,0,4], I, X), L).

I = _G180
X = _G181
L = [ (3, 4), (5, 4), (8, 4), (10, 4)] ;

No
*/

% }}}



