How to print numbers from 1 to 100 in Prolog?

The following code is the Prolog code, which gives all integers greater than 0. Each time I put ;in the interpreter, it gives the following number:

is_integer(0).
is_integer(X) :- is_integer(Y),X is Y+1.

Is there a way when it gives numbers from 0 to 100. When it reaches 100, it should stop.

+5
source share
2 answers

What a good poll. This illustrates very well how difficult it is to manage recursion with the minimal tools that Prolog defines. We must transfer our decisions to values ​​below a predetermined limit, restricting another unrelated search:

is_integer(0).
is_integer(X) :-
    is_integer(Y),
    ( Y >= 100, ! ; X is Y + 1 ).

Here's the trace output, limiting the range to 3 (i.e. ... Y >= 3, ! ; ...)

?- is_integer(X).
X = 0 ;
X = 1 ;
X = 2 ;
X = 3 ;
true.
+2
source

between/3 B, Ciao, SICStus (), SWI, YAP, XSB ().

?- between(0,100,X).
X = 0 ;
X = 1 ;
...
X = 100.

Prolog, s(X), . , 3:

?- nat_nat_sum(N,_,s(s(s(0)))).

:

nat_nat_sum(0,I,I).
nat_nat_sum(s(I),J,s(K)) :-
   nat_nat_sum(I,J,K).
+5

All Articles