Exactly one pair in Prolog list

前端 未结 2 440
终归单人心
终归单人心 2020-12-22 10:05

Does anybody know how I can go about determining/ensuring that there is exactly one duplicate element in a prolog list?

I am studying for a test.

相关标签:
2条回答
  • 2020-12-22 10:10

    Sort the list using sort/2. It removes duplicates, so if the sorted list is exactly one shorter, you had exactly one pair.

    one_duplicate(L) :-
        sort(L, Sorted),
        length(L, Len),
        length(Sorted, SortedLen),
        Len =:= SortedLen + 1.
    

    Finding the duplicate pair is another question altogether.

    0 讨论(0)
  • 2020-12-22 10:25
    one_duplicate(L) :-
        sort(L, Sorted),
        length(L, Len),
        length(Sorted, SortedLen),
        Len =:= SortedLen + 1.
    
    0 讨论(0)
提交回复
热议问题