2017-04-10 43 views
2

我正在嘗試處理任務,但是我確實在此部分停留。我需要做的是編寫一個名爲匹配的謂詞,使用三個參數,所有列表。第三個列表必須包含前兩個列表中包含相同值的位置的索引。我真的不知道從哪裏開始,所以任何幫助將不勝感激!如何根據序言中的2個列表創建索引值列表

+0

你能給什麼樣的第三列表將包含一個例子嗎?我想仔細檢查一下我的理解,避免將您發送到錯誤的方向。 – jcolemang

+1

您可以開始查看findall/3和nth1/3文檔 – CapelliC

回答

2

第三列表必須包含在其中前兩個列表包含相同的值的位置的索引。

第一重要:

第三列表必須包含在其中前兩個列表包含相同的值位置的索引。

使用統一。作謂語:

same_value(X, X). 

第三列表必須包含在其中前兩個列表包含相同的值位置的索引。

lists_same_value([X|_], [Y|_]) :- same_value(X, Y). % succeed 
lists_same_value([_|Xs], [_|Ys]) :-     % skip head 
    lists_same_value(Xs, Ys).      % recursive definition 

簡化和統一頭:

lists_same_value([X|_], [X|_]).  % succeed 
lists_same_value([_|Xs], [_|Ys]) :- % skip head element 
    lists_same_value(Xs, Ys).  % recursive definition 

第三列表必須包含指數,其中前兩個列表包含相同的值的立場。

爲當前索引添加累加器,當頭是相同元素時成功,並查看列表的其餘部分。

lists_same_value_index([X|_], [X|_], N, N).  % succeed 
lists_same_value_index([_|Xs], [_|Ys], N0, N) :- % skip head element 
    succ(N0, N1),        % next index 
    lists_same_value_index(Xs, Ys, N1, N).  % recursive definition 

實例化蓄電池:

lists_same_value_index(Xs, Ys, N) :- 
    lists_same_value_index(Xs, Ys, 0, N). 

用它來與bagof找到所有的解決方案:

?- bagof(N, lists_same_value_index([a,b,c,a,c,d,c], [a,c,c,a,d,d,c], N), Ns). 
Ns = [0, 2, 3, 5, 6]. 

,沒有這個是不一樣的,與兩個nth0解決方案當然,請參閱:

?- numlist(1, 100, L), time(bagof(N, lists_same_value_index(L, L, N), Ns)). 
% 314 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 1785481 Lips) 
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...], 
Ns = [0, 1, 2, 3, 4, 5, 6, 7, 8|...]. 

?- numlist(1, 100000, L), time(bagof(N, lists_same_value_index(L, L, N), Ns)). 
% 300,014 inferences, 0.052 CPU in 0.052 seconds (100% CPU, 5765387 Lips) 
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...], 
Ns = [0, 1, 2, 3, 4, 5, 6, 7, 8|...]. 

,然後用nth0

?- numlist(1, 100, L), time(findall(N, (nth0(N, L, E), nth0(N, L, E)), Ns)). 
% 2,181 inferences, 0.001 CPU in 0.001 seconds (100% CPU, 3329847 Lips) 
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...], 
Ns = [0, 1, 2, 3, 4, 5, 6, 7, 8|...]. 

?- numlist(1, 100000, L), time(findall(N, (nth0(N, L, E), nth0(N, L, E)), Ns)). 
% 1,667,166,681 inferences, 151.139 CPU in 151.244 seconds (100% CPU, 11030703 Lips) 
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...], 
Ns = [0, 1, 2, 3, 4, 5, 6, 7, 8|...]. 
1

由於@Capellic建議你可以使用的findall/3和NTH1/3謂詞:

common_elemnts_pos(L1,L2,Pos):- 
      findall(X, (nth0(X,L1,Elem), nth0(X, L2, Elem)) , Pos). 

上面只是說,找到所有位置X,在L1的元素是ELEM和元素在L2是ELEM,所以有相同的元素。

實施例:

?- common_elemnts_pos([1,3,4,5,7,9],[1,2,4,5,8,9],Pos). 
Pos = [0, 2, 3, 5]. 

這也很容易做到象純遞歸溶液:

common_elemnts_pos(L1,L2,Pos):- common_elemnts_pos(L1,L2,Pos,0). 

common_elemnts_pos([],_,[],_). 
common_elemnts_pos(_,[],[],_). 
common_elemnts_pos([H|T],[H|T1],[CurrentPos|T2],CurrentPos):- 
           N_CurrentPos is CurrentPos+1, 
           common_elemnts_pos(T,T1,T2,N_CurrentPos). 
common_elemnts_pos([H|T],[H1|T1],T2,CurrentPos):- 
           dif(H,H1), N_CurrentPos is CurrentPos+1, 
           common_elemnts_pos(T,T1,T2,N_CurrentPos). 

實施例:

?- common_elemnts_pos([1,3,4,5,7,9],[1,2,4,5,8,9],Pos). 
Pos = [0, 2, 3, 5] ; 
Pos = [0, 2, 3, 5] ; 
false. 
?- common_elemnts_pos([1,3,4,5,7,9],[1,2,4,5,8,9],Pos). 
Pos = [0, 2, 3, 5] ; 
false. 

在第一測試上面有兩個相同的解決方案因爲兩個基本情況都是有效的。如果你只想要一個你可以替換:

common_elemnts_pos([],_,[],_). 
common_elemnts_pos(_,[],[],_). 

與基本情況:

common_elemnts_pos([],[_|_],[],_). 
common_elemnts_pos(_,[],[],_). 
相關問題