我只是想弄清楚如何检查列表是否为空,我整理了一些检查列表长度的东西,另外还应该检查列表是否为空。
% 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.
Run Code Online (Sandbox Code Playgroud)
我是一名学生,所以我不一定需要一个直接的答案,我只是想弄清楚我做错了什么。
您不需要明确测试 L 是否为空列表;它的长度 > 0 确定了这一点。
并且您已经知道如何测试不为空的列表,因为您在listLength
:(L=[_|_]
即L
至少包含 1 个元素的列表)中使用了它。