我写了一个算法,并试图实现它在Prolog的,但我发现了什么是预期括号不起作用:什么是写在不退出括号之前全部完成。 下面是代码:
%1. If the first member of L1 is smaller than L2
% A. If the first member of L1 is not equal to Last
% Than: A.1 Add the first member of L1 to the first member of NL
% Begin recurssion on the resumption of L1, L2 resumption of NL
% and Last from L1.
% Else: A.2 begin the recursion on resumption of L1, L2 with the
% first member and L3 with the first member.
% 2. If the first member in L1 is equal to the first member of L2,
% Than: Start recursion on resumption of L1 and L2 (with its first
% member) and Last = *.
% 3. If the first member of L1 is bigger than the first membber of L2
% Than: begin recursion on L1 with the first member, resumption of
% L2 and Last = x. %(x != * only for debugging)
%
*/
make_list([X1|L1], [X2|L2], [X3|NewL], Last) :-
(
X1 < X2,
(
X1 \= Last, %A
X3=X1;
make_list(L1, [X2|L2], NewL, X1) %B
), %Why those parenthesis do not work as expected?
! %green cut
);
(
X1=X2,
make_list(L1, [X2|L2], [X3|NewL], *)
),
!
;
make_list([X1|L1], L2, [X3|NewL], *).
我的问题是如何使它按预期工作?为什么B
一度无法正常工作A
做? 毕竟这也是在同一个括号,例如:
?- make_list([6,6,10,20],[10,25,30],L, -).
L = [6|_G849] % (should be [6,20]).
EDIT1:make_list应该可以发现所有成员L1
不在L2
并把他们的NewL,而Last
卖场中的最后一个成员L1
是被解析。
EDIT2:无 - >允许(这是howmework)。 如果有人能告诉我该如何表达,如果再在别的序言,这可能是巨大的。