How do I find the longest list in a list of lists?

前端 未结 6 1029
说谎
说谎 2020-12-07 04:06

I have a list of lists, and I need to find the longest one of them. If there are more than one with the same length it\'s the same which it returns. Thanks.

6条回答
  •  醉话见心
    2020-12-07 05:05

    
    % Correct again.
    
    longest(LL,LX) :-
            findmax(Len,(append(_,[L|_],LL),length(L,Len)),MaxLen),
            append(_,[LX|_],LL),
            length(LX,MaxLen).
    
    findmax(V,P,Max) :-
            findall(V,P,L),
            max(L,Max).
    
    max([N],N) :- !.
    max([N|R],Max) :-
            max(R,Max2),
            max3(N,Max2,Max).
    
    max3(N,Max2,N) :- N > Max2,!.
    max3(N,Max2,Max2).
    

提交回复
热议问题