Prolog 列表中只有一对 [英] Exactly one pair in Prolog list

查看:16
本文介绍了Prolog 列表中只有一对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我如何确定/确保序言列表中只有一个重复元素吗?

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

我正在准备考试.

推荐答案

使用 sort/2 对列表进行排序.它会删除重复项,因此如果排序列表正好短了一个,那么您正好有一对.

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.

这篇关于Prolog 列表中只有一对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆