Prolog recrusive Algorithm

帅比萌擦擦* 提交于 2019-12-25 04:12:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!