How to find the number of inversions in a list in Prolog

前端 未结 3 1194
一整个雨季
一整个雨季 2021-01-22 22:50

As someone who\'s new to Prolog, I\'m looking to find out what a good way to count the number of inversions in a list.

I know how to flatten a matrix using flatten

3条回答
  •  执笔经年
    2021-01-22 23:00

    a possible definition, attempting to keep it as simple as possible:

    count_inversions(L, N) :-
        direction(L, D, L1),
        count_inversions(L1, D, 0, N).
    
    direction([A,B|L], D, [B|L]) :-
        A > B -> D = down ; D = up.
    
    count_inversions([_], _, N, N).
    count_inversions(L, D, M, N) :-
        direction(L, D, L1),
        !, count_inversions(L1, D, M, N).
    count_inversions(L, _, M, N) :-
        direction(L, D1, L1),
        M1 is M+1, count_inversions(L1, D1, M1, N).
    

    The direction/3 predicate compares a pair of elements, determining if they are in ascending/descending order. Such information is passed down visiting the list, and if it cannot be matched, a counter is incremented (an accumulator, starting from 0). When the visit stops (the list has only 1 elements, then no direction can be determined), the accumulated counter is 'passed up' to be returned at the top level call.

    I opted for a cut, instead of 'if/then/else' construct, so you can try to rewrite by yourself count_inversions/4 using it (you can see it used in direction/3). Beware of operators precedence!

    note: direction/3 ignores the ambiguity inherent when A =:= B, and assigns 'up' to this case.

    HTH

提交回复
热议问题