我想在prolog中写一个谓词 test(A,B) ,如果A小于或等于B,则为真 .
test(A,B)
查询示例(应返回true):
test(s(s(0)), s(s(s(0)))). test(s(s(s(0))), s(s(s(0)))).
这是我到目前为止编写的代码:
test(0,0). test(0, s(B)) :- nat(B). test(s(A),s(B)) :- test(A,B).
但它不起作用 .
我假设您使用自然数字为'0为0'且's(A)为A 1' . 然后你可以像这样写:
test(0,_). % everything is bigger or equal to 0. test(s(A),s(B)) :- test(A,B). % s(A) <= s(B) when A <= B
我们将与A和B一起走下去,直到:1)A变为0 - 表示A <= B,返回true2)B变为0而A不 - 表示B> A,返回false .
1 回答
我假设您使用自然数字为'0为0'且's(A)为A 1' . 然后你可以像这样写:
我们将与A和B一起走下去,直到:
1)A变为0 - 表示A <= B,返回true
2)B变为0而A不 - 表示B> A,返回false .