list - Prolog - 检查列表是否为空 []

标签 list prolog

我只是想弄清楚如何检查列表是否为空,我将一些东西放在一起检查列表的长度,并且另外应该检查列表是否不为空。

% Gives the length of a list.
listlength([]     , 0 ).
listlength([_|Xs] , L ):- 
    listlength(Xs,N), 
    L is N+1. 

% checks to see that the length of the list is greater than or equal to 3 and not empty.
check_length( [] ).
check_length( L  ):-
    listlength(L, Len),
    Len >= 3,
    L \== [].    %This is the bit of code I'm having problems with 
                  it should be false if the list is just [] or empty.

我是一名学生,所以我不一定需要直接的答案,我只是想找出我做错了什么。

最佳答案

您不需要显式测试 L 是否为空列表;它的长度 > 0 证明了这一点。

并且您已经知道如何测试列表是否为空,因为您在 listLength 中使用了它:L=[_|_](即 L 是一个至少包含 1 个元素的列表。

关于list - Prolog - 检查列表是否为空 [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33683838/

相关文章:

arrays - 如何找到列表中1的最长间隔[matlab]

python - 从Python中的递归函数返回

prolog - 事实在知识库中只出现一次

Prolog 解析树生成失败

Prolog - 将列表中的所有数字加 1

prolog - 在 Prolog 中获取解决方案列表

algorithm - 这种类型的二进制搜索有名称吗?

c# - 根据分组删除列表中除 1 个对象以外的所有对象

python - 使用给定项目拆分列表

prolog - CLP Prolog - 逻辑编程