我正在嘗試處理任務,但是我確實在此部分停留。我需要做的是編寫一個名爲匹配的謂詞,使用三個參數,所有列表。第三個列表必須包含前兩個列表中包含相同值的位置的索引。我真的不知道從哪裏開始,所以任何幫助將不勝感激!如何根據序言中的2個列表創建索引值列表
2
A
回答
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(_,[],[],_).
相關問題
- 1. 如何根據列表中的索引創建兩個新列表?
- 2. 根據sql中的另一個表列值創建表列
- 3. 如何根據現有列表的值創建新列表?
- 4. 根據另一個列表值對列表的索引值進行排序
- 5. 根據另一個列表的值創建一個列表c#
- 6. 如何在序言中創建列表
- 7. 如何根據檢索的數據創建多個列表?
- 8. 如何從列表中創建索引?
- 9. 通過組合2個列表中的每個索引來創建列表
- 10. 如何根據功能從另一個列表創建列表?
- 11. 列索引順序SQLite的創建表
- 12. 如何在python中創建一個數據框的索引和值的列表
- 13. Dynamicaly根據列值創建表
- 14. 根據列值創建列
- 15. 根據索引將Python列表拆分成幾個列表
- 16. 創建一個動態列表索引
- 17. 如何從列表1創建一個新的列表 - 列表2在Python中
- 18. 如何根據2個布爾表列填充列表視圖?
- 19. 如何根據索引值生成列表
- 20. 從索引列表和字符串列表中創建第三個列表
- 21. 序言:索引列表管理
- 22. 根據另外2個列表對齊2個python列表
- 23. Python - 根據索引在列表中重複列表
- 24. 根據索引列表,從列表中刪除項目
- 25. 如何使用數據在表格上創建序列號列索引
- 26. 如何根據之前的輸入創建下拉列表值?
- 27. 根據現有列中的值創建2列
- 28. SQL:如何根據同一表中的其他列的值創建新列
- 29. Python從列表中的特定索引創建列表
- 30. 根據另一個列表中唯一值的索引從一個列表中獲取相應的值 - Perl
你能給什麼樣的第三列表將包含一個例子嗎?我想仔細檢查一下我的理解,避免將您發送到錯誤的方向。 – jcolemang
您可以開始查看findall/3和nth1/3文檔 – CapelliC