我正在使用以下代码按最后一个元素快速排序四倍。当我自己调用 productSort 时,它会按预期对列表进行排序。但是,当我在使用 makeList 函数后运行它时,它不起作用并在我自己使用它时返回运行。但是,当我尝试使用它时,它会显示“错误:> = / 2:参数没有充分实例化异常:(2,359) splitListProduct(_G35274, [2, 3, 5, 6], _G90741, _G90651) ?”。
makeList(_N, 50, _P, []):-!.
makeList(N, X, Y, [[X, Y, Sum, Product] | L2]) :-
Sum is X + Y,
Sum =< N,
Product is X * Y,
Hello is Y+1,
write([X, Y, Sum, Product]),nl,
makeList(N, X, Hello, L2).
makeList(N, X, Y, L) :-
write('here'),nl,
write(X),nl,
X == 49, !.
makeList(N, X, Y, L) :-
write('Y'), write(Y),nl,
write('X'), write(X),nl,
Sum is X + Y,
Sum > N,
NewX is X + 1,
NewY is X + 2,
makeList(N, NewX, NewY , L).
proper_length(List, Length) :-
is_list(List),
length(List, Length).
run(N, X, Y, L) :- makeList(N, X, Y, L), productSort(L,SortedL).
productSort([[X,Y,S,P|_]|Xs],Ys) :-
splitListProduct(Xs,[X,Y,S,P],Left,Right), /*Split it, we have a nested list here as X*/
productSort(Left,Ls),
productSort(Right,Rs),
append(Ls,[[X,Y,S,P]|Rs],Ys),!.
productSort([],[]).
splitListProduct([[X2,Y2,S2,P2]|Xs],[X1,Y1,S1,P1|_],[[X2,Y2,S2,P2]|Ls],Rs) :- /* Y is the nested list*/
P1 >= P2, splitListProduct(Xs,[X1,Y1,S1,P1],Ls,Rs).
splitListProduct([[X2,Y2,S2,P2]|Xs],[X1,Y1,S1,P1|_],Ls,[[X2,Y2,S2,P2]|Rs]) :-
P2 > P1, splitListProduct(Xs,[X1,Y1,S1,P1],Ls,Rs).
splitListProduct([],Y,[],[]):-!.
append([],Ys,Ys).
append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).