使用meta-predicatetexclude/3
結合的 reified term equality predicate (=)/3
!
首先,我們嘗試使用(=)/3
直接...
?- texclude(=((1,V)), [(1,1),(1,2),(3,2)], KVs).
KVs = [ (1,2),(3,2)], V=1 ;
KVs = [(1,1), (3,2)], V=2 ;
KVs = [(1,1),(1,2),(3,2)], dif(V,1), dif(V,2).
不太!對於我們的下一次嘗試,我們將使用lambda expressions。
:- use_module(library(lambda)).
讓我們查詢一次與texclude/3
,一旦與tinclude/3
---,並曾經與tpartition/4
:
?- texclude( \ (K,_)^(K=1), [(1,1),(1,2),(3,2)], Fs).
Fs = [(3,2)]. % succeeds deterministically
?- tinclude( \ (K,_)^(K=1), [(1,1),(1,2),(3,2)], Ts).
Ts = [(1,1),(1,2)]. % succeeds deterministically
?- tpartition(\ (K,_)^(K=1), [(1,1),(1,2),(3,2)], Ts,Fs).
Ts = [(1,1),(1,2)], Fs = [(3,2)]. % succeeds deterministically
好吧!如果列表項是後texclude/3
調用,我們得到相同的解決方案嗎?
?- texclude(\ (K,_)^(K=1), [A,B,C], Fs), A = (1,1), B = (1,2), C = (3,2).
A = (1,1), B = (1,2), C = (3,2), Fs = [(3,2)] ; % succeeds with choice point
false.
是的!最後,考慮以下相當一般查詢:
?- texclude(\ (K,_)^(K=1), [A,B], Fs).
Fs = [ ], A = ( 1,_A1), B = ( 1,_B1) ;
Fs = [ B], A = ( 1,_A1), B = (_B0,_B1), dif(_B0,1) ;
Fs = [A ], A = (_A0,_A1), B = ( 1,_B1), dif(_A0,1) ;
Fs = [A,B], A = (_A0,_A1), B = (_B0,_B1), dif(_A0,1), dif(_B0,1).
注意,上述目標的限制所有列表項有形式(_,_)
。因此,以下查詢失敗:
?- texclude(\ (K,_)^(K=1), [x,_], _).
false.
「my_delete/3」的所有子句都是遞歸的,這太多了:) – repeat