我的任务是定义一个谓词,检查给定列表是否是序列的一部分。谓词有三个成员。前两个列表是序列的开头。每个其他元素L(N + 2),N> = 0是L(N)和反转L(N + 1)的级联。我必须检查第三个参数是否是序列的一部分。
end
这些是一些例子:
@Mapper(imports = { NotificationPeriodHelper.class })
public interface ISchedulePayloadMapper
{
@Mappings({
@Mapping(target = "name", source = "scheduleName"),
@Mapping(target = "scheduler", source = "schedulerName"),
@Mapping(target = "notificationPeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, notificationFrom, notificationTo))"),
@Mapping(target = "schedulePeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, scheduleFrom, scheduleTo))")
})
SchedulePayload map(XmlSchedule xmlSchedule, Lecture lecture);
}
L1 = [1]和L2 = [2]的示例序列; L3 = [1,2]; L4 = L2 +反转(L3)= [2,2,1]; L5 = L3 +反转(L4)= [1,2,1,2,2]
预期结果:
my-append([], L, L).
my-append([H|T], L, [H|R]) :- my-append(T, L, R).
list-len([], 0).
list-len([_|T], N) :- list-len(T, N1), N is N1 + 1.
my-reverse([], []).
my-reverse([H|T], R) :- my-reverse(T, R1), my-append(R1, [H], R).
compare-list([], []).
compare-list([H1|A], [H2|B]) :- H1 =:= H2, compare-list(A, B).
fib-list(A, B, C) :-
my-reverse(B, B1),
list-len(A, LenA),
list-len(B1, LenB1),
my-append(A, B1, C1),
list-len(C, LenC),
list-len(C1, LenC1),
LenC1 =:= LenA + LenB1,
(
(LenC > LenC1, !);
(LenC =:= LenC1, compare-list(C1, C));
(LenC < LenC1, fib-list(B, C1, C))
).
问题是第二次和第三次测试的结果是真的,我不知道为什么。
答案 0 :(得分:2)
你过分思考问题并强制使用Prolog,就像使用C语言或其他命令式语言一样使用它。
首先描述你的谓词。您的fib-list(A, B, C)
表示 C
的后面跟B
附加的A
相反。所以写下:
fib-list(A, B, C) :-
my-reverse(B, B1), % B1 is reverse of B
my-append(A, B1, C1), % C1 is reverse of B (B1) appended to A
my-append(C1, _, C). % C is something/anything appended to C1
% (C begins with C1)