Prolog - Announcement of Arithmetic Articles

A simple question: how can I declare a sentence that will create the number indicated by +1 +2 and +3? I 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 tried using "is" to make it evaluate, but it does not seem to work. Any help would be appreciated.

+3
source share
1 answer

Try the following:

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

or

addup(X, X+1).

and you should ask a question (2, X)

then X should be 3. If you want to parameterize your add parameter, just do this:

addup(X, Y, X + Y).

and ask by adding (5, 6, X).

+2
source

Source: https://habr.com/ru/post/1735466/


All Articles