Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog - Declaring arithmetic clauses

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).
addup(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.

like image 340
Dororo Avatar asked Feb 20 '26 04:02

Dororo


1 Answers

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).

like image 67
anthares Avatar answered Feb 24 '26 11:02

anthares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!