小修复,它应该工作:
% remove_same_indicator(+Callable,+List,-List)
remove_same_indicator(_, [], []).
remove_same_indicator(X, [Y|L], R) :-
functor(X, F, N),
functor(Y, F, N),
!,
remove_same_indicator(X, L, R).
remove_same_indicator(X, [Y|L], [Y|R]) :-
remove_same_indicator(X, L, R).
试一试吧:
?- remove_same_indicator(a(_), [a(1),a(2),a(3),b(1)], R).
R = [b(1)]
优于 findall 解决方案,不会丢失变量。例如,可以这样做:
?- remove_same_indicator(a(_), [a(A),a(B),a(C),b(A)], R).
R = [b(A)]
但是通过 findall 解决方案,我们得到:
?- L=[a(A),a(B),a(C),b(A)], findall(X, (member(X,L),\+ (X = a(_))), R).
L = [a(A), a(B), a(C), b(A)],
R = [b(_I)]
b 的参数不再绑定到 A,因为 findall 创建了副本并因此创建了新变量。
再见
functor/3 是 ISO,也在 Amzi 中!
http://www.amzi.com/manuals/amzi/pro/ref_manipulating_terms.htm#functorTermFunctorN