我是Prolog的新手,我有一个问题。我有两个条款:
perform([X,Y], move(west), [NX,Y]) :- NX is X - 1, !.
perform([X,Y], move(south), [X,NY]) :- NY is Y - 1, !.
X和Y是我的坐标。我想要的就是这样:
perform([X,Y], move(west), [NX,Y]):- NX is X - 1, !.
if NX == abs(Y)
perform([X,Y], move(west), [NX,Y]):- NX is X - 1, !.
else
perform([X,Y], move(south), [NX,NY]):- NY is Y - 1, !.
基本上移动(向西)并且如果我的X坐标是someValue move(向南)。
答案 0 :(得分:0)
您可以尝试类似
的内容perform([X,Y], move(west), [NX,Y]) :- NX is X - 1, !.
perform([X,Y], move(south), [X,NY]) :- NY is Y - 1, !.
NX == abs(Y) -> perform([X,Y], move(west), [NX,Y]);
perform([X,Y], move(south), [NX,NY]).
或
NX == abs(Y) -> NX is X - 1;
NY is Y - 1.
答案 1 :(得分:0)
有许多方法可以实现您需要的条件操作。
示例1 - 使用数学表达式进行的条件(不是最可读的条件):
perform2([X,Y], move(west), [NX,NY]) :-
NX is X - 1,
NY is Y - 1*0^(NX-abs(Y)),
!.
示例2 - 使用运算符->/2
和;/2
生成的条件:
perform3([X,Y], move(west), [NX,NY]) :-
NX is X - 1,
( NX =:= abs(Y) -> NY is Y - 1; NY = Y ),
!.
示例3 - 使用单独的条款进行的条件:
perform4([X,Y], move(west), [NX,NY]) :-
NX is X - 1,
NX =:= abs(Y),
NY is Y - 1,
!.
perform4([X,Y], move(west), [NX,Y]) :-
NX is X - 1,
NX =\= abs(Y),
!.
对于所有上述谓词,输入和输出如下所示:
?- perform4([1,0], move(west), X).
X = [0, -1].
?- perform4([2,0], move(west), X).
X = [1, 0].