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

前端 未结 6 1010
说谎
说谎 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:00

    To have the length of longest list:

    %sample: longest([[2,1,3],[7,5],[1,8,2,3,1],[2,7,1,4]],L,LEN).
    
    longest([L], L, _) :-
       !.
    longest([H|T], H, _) :-
       length(H, N),
       longest(T, X, N),
       length(X, M),
       N > M,
       !.
    longest([_|T], X, LEN) :-
       length(X, LEN),
       longest(T, X, LEN),
       !.
    

提交回复
热议问题