问题
foo(0,Y,Z) :- Z is Y.
foo(X,0,Z) :- Z is X.
foo(X,Y,Z) :- X>=Y, M1 is X-2, foo(M1, Y, Zx), Z is Zx + Y.
foo(X,Y,Z) :- Y<X, N1 is Y-3, foo(X, N1, Zx), Z is Zx + X.
So this is my program and this is what i'm trying to accomplish
𝑓𝑜𝑜(𝑥, 𝑦) = {
𝑥 𝑖𝑓 𝑦 ≤ 0
𝑦 𝑖𝑓 𝑥 ≤ 0
𝑥 + 𝑓𝑜𝑜(𝑥 − 2, 𝑦) 𝑖𝑓 𝑥 ≥ 𝑦
𝑦 + 𝑓𝑜𝑜(𝑥, 𝑦 − 3) 𝑖𝑓 𝑥 < 𝑦 }
Why does my program not output anything? This is what i think i'm saying -
If X = 0, foo(0,Y,Z), than return Z as Y. If Y = 0, foo(0,Y,Z), than return Z as X.
if X>=Y, than do foo again and once that returns than return Z as Zx + Y if X
Am i correct in my thinking?
回答1:
I remember that in your function is
𝑥 𝑖𝑓 𝑦 ≤ 0
𝑦 𝑖𝑓 𝑥 ≤ 0
that is "less or equal to zero" not "equal to zero".
So I suppose that
foo(0,Y,Z) :- Z is Y.
should be
foo(X, Y, Y) :- X =< 0.
and that
foo(X,0,Z) :- Z is X.
should be
foo(X, Y, X) :- Y =< 0.
来源:https://stackoverflow.com/questions/40920830/prolog-recrusive-algorithm