开发者

Prolog - Declaring arithmetic clauses

开发者 https://www.devze.com 2022-12-22 10:13 出处:网络
A simple question, how would I go about declaring a clause which would produce the number specified +1 +2 and +3? I have tried:

A simple question, how would I go about declaring a clause which would produce the number specified +1 +2 and +3? I have tried:

addup(Thenumber,Thenumber+1).
add开发者_Python百科up(Thenumber,Thenumber+2).
addup(Thenumber,Thenumber+3).

but when I run it with say, Thenumber=5, it just returns 5+1 5+2 5+3. I have tried using 'is' to force it to evaluate but it doesn't seem to work. Any help would be appreciated.


Try this:

addup(X, Y) :- Y is X + 1.

or

addup(X, X+1).

and you question should be addup(2, X)

then X should be 3. If you want to parametrize your addup parameter just make it:

addup(X, Y, X + Y).

and ask with addup(5, 6, X).

0

精彩评论

暂无评论...
验证码 换一张
取 消