2017-04-10 75 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(_,[],[],_). 
相关问题