名單我很新的序言,我需要一些幫助,一個小問題:拆分在序言
我想在兩個列表分割的夫妻列表。第一個列表包含所有具有給定鍵的對,第二個列表包含所有其他對象。
這是我的代碼至今:
splitList([],_,[],[]).
splitList([(A,X)|Rest], B, [Elem1|List1], [Elem2|List2]):-
(
A == B
->
Elem1 = (A,X),
splitList(Rest, B, List1, [Elem2|List2])
;
Elem2 = (A,X),
splitList(Rest, B, [Elem1|List1], List2)
).
當我嘗試測試它,這就是我得到:
[trace] [3] 143 ?- splitList([(1,yellow),(1,blue),(2,yellow),(2,blue)],1,X,Y).
Call: (37) splitList([ (1, yellow), (1, blue), (2, yellow), (2, blue)], 1, _G4821, _G4822) ? creep
Call: (38) 1==1 ? creep
Exit: (38) 1==1 ? creep
Call: (38) _G4928= (1, yellow) ? creep
Exit: (38) (1, yellow)= (1, yellow) ? creep
Call: (38) splitList([ (1, blue), (2, yellow), (2, blue)], 1, _G4929, [_G4931|_G4932]) ? creep
Call: (39) 1==1 ? creep
Exit: (39) 1==1 ? creep
Call: (39) _G4940= (1, blue) ? creep
Exit: (39) (1, blue)= (1, blue) ? creep
Call: (39) splitList([ (2, yellow), (2, blue)], 1, _G4941, [_G4931|_G4932]) ? creep
Call: (40) 2==1 ? creep
Fail: (40) 2==1 ? creep
Call: (40) _G4931= (2, yellow) ? creep
Exit: (40) (2, yellow)= (2, yellow) ? creep
Call: (40) splitList([ (2, blue)], 1, [_G4949|_G4950], _G4932) ? creep
Call: (41) 2==1 ? creep
Fail: (41) 2==1 ? creep
Call: (41) _G4958= (2, blue) ? creep
Exit: (41) (2, blue)= (2, blue) ? creep
Call: (41) splitList([], 1, [_G4949|_G4950], _G4959) ? creep
Fail: (41) splitList([], 1, [_G4949|_G4950], _G4959) ? creep
Fail: (40) splitList([ (2, blue)], 1, [_G4949|_G4950], _G4932) ? creep
Fail: (39) splitList([ (2, yellow), (2, blue)], 1, _G4941, [_G4931|_G4932]) ? creep
Fail: (38) splitList([ (1, blue), (2, yellow), (2, blue)], 1, _G4929, [_G4931|_G4932]) ? creep
Fail: (37) splitList([ (1, yellow), (1, blue), (2, yellow), (2, blue)], 1, _G4821, _G4822) ? creep
false.
明顯的解決辦法應該是X = [(1 ,黃色),(1,藍色)]和Y = [(2,黃色),(2,藍色)],但我得到了錯誤。 有人能告訴我我做錯了什麼嗎?
由於提前,
WALLE
謝謝,它現在可行! – Walle