问题
I have written the following code:
nat(0).
nat(s(X)) :- nat(X).
divide(0,_,0).
divide(X,Y,D) :- X@<Y, D is 0.
divide(X,s(0),X).
divide(_,0,undefined) :- !.
Everything is right up to here. but what should i write, to calculate the division of two other naturals? for example
divide(s(s(s(s(s(s(s(s(0)))))))),s(s(0)),D).???
回答1:
I think the easiest way would be to define subtraction and then apply it recursively with a counter. I'm not able to run this right now but I assume it'd look something like this:
nat(0).
nat(s(X)) :- nat(X).
divide(_,0,_):- !, fail.
divide(X,Y,R):-
iter_subtract(X,Y,0,R).
iter_subtract(X,Y,N,N):- X@<Y, !.
iter_subtract(X,Y,C,R):-
subtract(X,Y,N),
D = s(C),
iter_subtract(N,Y,D,R).
subtract(A,0,A):-!.
subtract(s(X),s(B),R):-
subtract(X,B,R).
回答2:
you can easily divide two numbers as following:
mydiv(0,_,0).
mydiv(_,0,undefined):- !.
mydiv(X,Y,D) :- X >= Y , Z is X - Y , mydiv(Z,Y,M) , D is M + 1 .
output:
?- mydiv(10,4,Q).
false.?- mydiv(100,2,Q).
Q = 50 .
to run with debug info:
mydiv(0,_,0) :- write('mydiv(0,_,0)\n').
mydiv(_,0,undefined):- write('mydiv(_,0,undefined)\n'),!.
mydiv(X,Y,D) :- write('mydiv('),write(X) , write(','), write(Y) , write(',') , write(D) , write(')') , nl,
X >= Y , Z is X - Y , mydiv(Z,Y,M) , D is M + 1 .
output:
?- mydiv(20,2,Q).
mydiv(20,2,_G3941)
mydiv(18,2,_L1420)
mydiv(16,2,_L1435)
mydiv(14,2,_L1450)
mydiv(12,2,_L1465)
mydiv(10,2,_L1480)
mydiv(8,2,_L1495)
mydiv(6,2,_L1510)
mydiv(4,2,_L1525)
mydiv(2,2,_L1540)
mydiv(0,__,0)
Q = 10 .
with natural function:
natural(0).
natural(X) :- X < 0 , !, fail.
natural(X) :- Y is X - 1 , natural(Y).
mydiv(0,_,0) :- write('mydiv(0,_,0)\n').
mydiv(_,0,undefined):- write('mydiv(_,0,undefined)\n'),!.
mydiv(X,Y,D) :- write('mydiv('),write(X) , write(','), write(Y) , write(',') , write(D) , write(')') , nl,
natural(X), natural(Y), X >= Y , Z is X - Y , mydiv(Z,Y,M) , D is M + 1 .
来源:https://stackoverflow.com/questions/30016859/division-two-naturals-in-prolog