更新时间:2023-02-19 22:58:10
你的代码有几个问题:
filter([],[],0).
在处理任何不以 0 作为最小值的列表时不会统一,这不是您想要的.无论结束递归的最小值如何,您都希望它统一.filter([H1|T1],[H2|T2],Z)
的方式及其主体将使两个列表始终具有相同数量的元素,当事实上,第二个应该至少少一个.filter([],[],0).
will not unify when working with any list that does not have 0 as its minimum value, which is not what you want. You want it to unify regardless of the minimum value to end your recursion.filter([H1|T1],[H2|T2],Z)
and its body will make it so that the two lists always have the same number of elements, when in fact the second one should have at least one less.filter/3
的正确实现如下:
filter([],[],_).
filter([H1|T1],L2,Z):-
+ number(H1),
write("ERROR: List parameter contains a non-number element"),
!;
H1 = Z -> filter(T1,T2,Z), L2 = [H1|T2];
filter(T1,L2,Z).