M
M
MajorTom692017-11-21 01:10:35
Prolog
MajorTom69, 2017-11-21 01:10:35

How to generate a sequence like "0, 1, -1, 2, -2, 3, -3..." in Prolog?

It is necessary to generate elements of such a sequence "0, 1, -1, 2, -2, 3, -3 ...". I just got to this, but the result is crashing:

num(1).
num(X) :- num(Y), X is -Y, X < 0.
num(X) :- num(Y), X is -Y + 1, X > 0.

conclusion:
?- num(X).
X = 1 ;
X = -1 ;
ERROR: Out of local stack
   Exception: (1,763,389) num(_6002) ?

I ask for help, because I have already boiled.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Paramonov, 2017-11-21
@varagian

Here the last answer explains why it doesn't work and shows solutions:
https://stackoverflow.com/questions/19979548/predi...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question