我正在使用以下代碼來通過最後一個元素對四元組進行快速排序。當我自己調用productSort時,它按預期對列表進行分類。但是,當我使用makeList函數運行它時,它不起作用,並且在我自己使用它時運行返回。但是,當我嘗試並使用它時,它會顯示「錯誤:> =/2:參數沒有充分實例化」 異常:(2,359)splitListProduct(_G35274,[2,3,5,6],_G90741,_G90651)?「。Prolog,函數自己運行,但是當被另一個函數調用時,它說的參數沒有被充分實例化
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).