1
如果給定版本具有給定的順序關係,則編寫謂詞complst/3。順序將是:lt,le,gt,ge或eq(小於,小於或等於,大於,大於或等於,等於)中的一個,謂詞應該對所有適用的關係。例如,如何用Prolog編寫complst/3謂詞?
?- complst([2,3,4], [2,3,5], C).
C = lt ;
C = le .
?- complst([1,2,3,4], [1,1,8], C).
C = gt ;
C = ge .
爲了獲得目標,我寫了一個斷言:
complst([], [], Cmp) :-
Cmp = eq, !.
complst([A], [B], Cmp) :-
A > B,
Cmp = gt.
complst([A], [B], Cmp):-
A < B,
Cmp = lt.
complst([A], [B], Cmp):-
A >= B,
Cmp = ge.
complst([A], [B], Cmp):-
A =< B,
Cmp = le.
complst([], [B], Cmp):-
Cmp = le, !.
complst([], [B], Cmp):-
Cmp = lt, !.
complst([A], [], Cmp):-
Cmp = ge, !.
complst([A], [], Cmp):-
Cmp = gt, !.
complst([X|XS], [Y|YS], Cmp):-
X > Y,
cmp_list([X], [Y], Cmp).
complst([X|XS], [Y|YS], Cmp):-
X < Y,
cmp_list([X], [Y], Cmp).
complst([X|XS], [Y|YS], Cmp):-
X == Y,
cmp_list(XS, YS, Cmp).
但問題是,輸出將永遠不會停止,除非我按enter鍵。 任何人都可以幫我解決這個問題嗎? 在此先感謝!