【发布时间】:2015-03-10 23:01:43
【问题描述】:
我需要写一个谓词next(X, List1, List2),它返回List2,一个直接跟在X之后的元素数组。
例如,
next(v1,[v1,v2,v3,v1,v2,v1,v5],L) returns L=[v2,v2,v5]
next(b,[b,k,m,b,j],L) returns L=[k,j]
next(s,[s,b,c,d,e,f,s,c,s,g],L) returns L=[b,c,g]
....
我知道必须使用递归和尾部。
我想我知道谓词的逻辑以及应该如何工作,但我无法让它工作。如果用户输入next(a,[a,b,c,a,b,c],L).
[a,b,c,a,b,c]
%if first letter is a, put the letter after it in array L, if not - remove first letter.
%first letter is a, put b in array L, remove a from initial array.
[b,c,a,b,c]
%if first letter is a, put the letter after it in array L, if not - remove first letter.
% first letter is b, it is not a, so remove it from initial array
[c,a,b,c]
%if first letter is a, put the letter after it in array L, if not - remove first letter.
% first letter is c, it is not a, so remove it from initial array
[a,b,c]
%if first letter is a, put the letter after it in array L, if not - remove first letter.
% first letter is a, put b in array L, remove a from initial array.
[b,c]
%if first letter is a, put the letter after it in array L, if not - remove first letter.
% first letter is b, it is not a, so remove it from initial array
[c]
%if first letter is a, put the letter after it in array L, if not - remove first letter.
% first letter is c, it is not a, so remove it from initial array
这就是我所拥有的:
next(X, List1, List2):-
next(X,[X,X2|List1],X2).
我知道方括号中的部分是错误的。
更新 #1:
/* X is the head of the list */
next(X, [X,Y|T1], [Y|T2]) :-
next(X, [Y|T1], T2).
/* X is not the head of the list*/
next(X, [_|T1], [T2]) :-
next(X, T1, T2).
/* T1 contains only one element */
next(X, _, [T2]):-
true.
/* T1 is empty */
next(X,[T2]):-
true.
更新 #1 的跟踪日志:
1 ?- 跟踪。 真的。
[trace] 1 ?- next(a,[a,c,d,e,f,a,g],S).
Call: (6) next(a, [a, c, d, e, f, a, g], _G4792) ? creep
Call: (7) next(a, [c, d, e, f, a, g], _G4880) ? creep
Call: (8) next(a, [d, e, f, a, g], _G4885) ? creep
Call: (9) next(a, [e, f, a, g], _G4888) ? creep
Call: (10) next(a, [f, a, g], _G4891) ? creep
Call: (11) next(a, [a, g], _G4894) ? creep
Call: (12) next(a, [g], _G4898) ? creep
Call: (13) next(a, [], _G4903) ? creep
Exit: (13) next(a, [], [_G4906]) ? creep
Exit: (12) next(a, [g], [[_G4906]]) ? creep
Exit: (11) next(a, [a, g], [g, [_G4906]]) ? creep
Exit: (10) next(a, [f, a, g], [[g, [_G4906]]]) ? creep
Exit: (9) next(a, [e, f, a, g], [[[g, [_G4906]]]]) ? creep
Exit: (8) next(a, [d, e, f, a, g], [[[[g, [_G4906]]]]]) ? creep
Exit: (7) next(a, [c, d, e, f, a, g], [[[[[g, [_G4906]]]]]]) ? creep
Exit: (6) next(a, [a, c, d, e, f, a, g], [c, [[[[g, [_G4906]]]]]]) ? creep
S = [c, [[[[g, [_G4906]]]]]]
我从这些资源中浏览了基本级别的 prolog 练习列表: http://www.ic.unicamp.br/~meidanis/courses/problemas-prolog/ http://www.anselm.edu/homepage/mmalita/culpro/index.html
【问题讨论】:
-
以
next(X, [X,Y|T1], [Y|T2]) :- next(X, [Y|T1], T2).开头 当X不在第二个参数的开头时,您需要另一个子句,并且当第二个参数为空或只有一个元素。 -
@lurker 1.) 当 x 不在头部时,如何编写子句?我可以为 x,y 是尾巴时写一个子句。 2.)关于基本情况,我想我已经弄清楚了,但是我在其中返回什么?在上面发布我的代码。
-
@lurker 我快完成了。现在谓词返回所有正确的元素,还有一些不需要的括号,你可以在我发布的跟踪日志中看到。
-
如果
X和Y不同,请检查dif(X, Y)是否正确。