Exactly one pair in Prolog list

2019-01-29 10:43发布

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.

标签: list prolog pair
2条回答
手持菜刀,她持情操
2楼-- · 2019-01-29 11:29

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-29 11:44
one_duplicate(L) :-
    sort(L, Sorted),
    length(L, Len),
    length(Sorted, SortedLen),
    Len =:= SortedLen + 1.
查看更多
登录 后发表回答