Jack23
Jack23

Reputation: 1396

FInd maximun in a List of List

I have a list of list like this:

matrix_max([[1, 2, 3], [8, 1, 4], [3, 5, 9]], Max).

I need to determine the maximum value between all the lists ( Max = 9 ).

I don't understand How can I do to check the value in other lists thant the first.

// there i found the max value of a list
my_max([], R, R). 
my_max([X|Xs], WK, R):- X >  WK, my_max(Xs, X, R). 
my_max([X|Xs], WK, R):- X =< WK, my_max(Xs, WK, R).
my_max([X|Xs], R):- my_max(Xs, X, R). 


// then I should control the maximum value for all the lists.
matrix_max([Head | Rest], Max) :-
    matrix_max([Head | Rest], 0, Max).

matrix_max([Head | Rest], Value, Max) :-
    my_max(Head, Max_List),
    Max_List > Value, 
    Max is Max_List,
    matrix_max(Rest,Max_List, Max).

How can I do?

Thank you

EDIT1.

So my code is:

matrix_max([], Max, Max).
matrix_max([Head | Rest], Value, Max) :-
    my_max(Head, Max_List), 
    Max is max(Max_List, Value),
    matrix_max(Rest,Max_List, Max).

matrix_max([Head | Rest], Max) :-
    my_max(Head, M),
    matrix_max(Rest, M, Max).

 my_max([X|L], M) :-
    my_max(L, X, M).

my_max([], R, R). 
my_max([X|Xs], WK, R):-
    V is max(X, WK),
    my_max(Xs, V, R).

I tried this: matrix_max([[1, 2, 3], [8, 1, 4], [3, 5, 9]], Max).

false.

Upvotes: 1

Views: 57

Answers (2)

Reema Q Khan
Reema Q Khan

Reputation: 878

Here's my answer:-

max([],0).
max([H|T],Max):-
    max(T,TMax),
    H>TMax,
    Max is H.
max([H|T],Max):-
    max(T,TMax),
    H=<TMax,
    Max is TMax.

matrix_max(List, Max):-
    findall(V1,member([V1,_,_],List),V1List),
    findall(V2,member([_,V2,_],List),V2List),
    findall(V3,member([_,_,V3],List),V3List),
    append(V1List,V2List,VR1),
    append(V3List,VR1,VR2),
    max(VR2,Max).

?- matrix_max([[1, 2, 3], [8, 1, 4], [3, 5, 9]], Max).
Max = 9

?- matrix_max([[1, 2, 3], [8, 1, 4], [3, 5, 9], [5,8,66]], Max).
Max = 66

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476604

You did not specify a casee for Max_List <= Value. But you do not need to branch for the two cases. Prolog's is/2 [swi-doc] can evaluate an expression with max(…, …)`, indeed:

?- M is max(1,2).
M = 2.

We thus can calculate the maximum of the Max_list and Value:

matrix_max([], Max, Max).
matrix_max([Head | Rest], Value, Max) :-
    my_max(Head, Max_List), 
    MaxCur is max(Max_List, Value),
    matrix_max(Rest, MaxCur, Max).

The matrix_max/2 predicate can not use 0 as initial accumulator, since the values can all be smaller than zero. You should first call my_max/2 on the first row:

matrix_max([Head | Rest], Max) :-
    my_max(Head, M),
    matrix_max(Rest, M, Max).

You furthermore need to cover the case where the list is empty, so the first clause.

In a similar way you can simplify my_max/3:

my_max([X|L], M) :-
    my_max(L, X, M).

my_max([], R, R). 
my_max([X|Xs], WK, R):-
    V is max(X, WK),
    my_max(Xs, V, R).

You can implement both my_max and matrix_max as a "fold pattern" with foldl/4:

max2(X1, X2, M) :-
    M is max(X1, X2).

my_max([X|Xs], M) :-
    my_max(Xs, X, M).

my_max(Xs, M0, M) :-
    foldl(max2, Xs, M0, M).

matrix_max([R|Rs], M) :-
    my_max(R, RM),
    foldl(my_max, Rs, RM, M).

Upvotes: 2

Related Questions