0
votes

I have a simple Prolog program that prints all the values from X to 10.

print_(X) :- X <= 10, X1 is X + 1, write(X), print_(X1).

So for example print_(5) would return: 5 6 7 8 9 10 False.

However I would like it to return True at the end. I have tried all sorts, is there anyway of doing this?!

Thanks a lot.

3
I assume you're code really has X =< 10 not X <= 10 (which would be a syntax error). - lurker

3 Answers

3
votes

You could also do it using a termination case that succeeds:

print_(X) :-
    X =< 10,
    X1 is X + 1,
    write(X), nl,
    print_(X1), !.
print_(X) :- X > 10.

Then you get:

| ?- print_(5).
5
6
7
8
9
10

yes
| ?-

Note that this solution, as well as the solution @larsmans presented, will succeed on cases like print_(11)., but just won't display any values.


ADDENDUM

@CapelliC presented a very concise solution which will present failure if the initial argument is out of range. Another option, using simple predicates might be:

print_(X) :-
    X =< 10,
    X1 is X + 1,
    write(X), nl,
    (print_(X1) -> true ; true).

The last line says, "I don't care whether the query print_(X1) succeeds or fails, I want to succeed and move on." The print_/2 predicate, however, will fail if the argument is initially out of range.

This yields:

| ?- print_(5).
5
6
7
8
9
10

yes
| ?-

And:

| ?- print_(11).

no
| ?-
2
votes

To avoid the pitfall pointed out in mbratch answer, an alternative could be

print_(X) :- X =< 10, forall(between(X,10,N), write(N)).
1
votes

Wrap it in another predicate that negates the result.

print_loop(X) :- \+ print_(X).