Re: Loop for printing numbers in Prolog



George schrieb:
Dear All,

I have written the following code for printing numbers from a given number up to 21:

georgetry(X):-
write(X),write(nl),
process(X,Y),
repeat,
process(Y,L),
test(L).

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

process(Z,Y):-
write(Z),
increment(Z,Y).

test(Y):-
Y = 21.

When I run it naturally I get 13 printed all the time, because I am using variables that are bound already when calling process(X,Y) inside the loop (I can not figure out a way of not doing it that way though).

I'd rather say it's because of backtracking. After calling process(Y, L)
L is unified with the next number thus it's bound to it. Then you call
test(L) which fails, thus backtracking goes back to a step that can
be proofed which is repeat. During backtracking bindings are released.
Thus Y and L have the same bindings as they had entering the repeat
loop. The only thing which isn't revoked are side effects. Therefor
repeat works as far as I know (which isn't very far) only with
side effects.


I know it can be easily done with a recursive algorithm, but I was just wondering whether looping is also an option.

The only thing I could imagine would be with the help of database
manipulation which is also done by side effects.

regards
Stephan
.



Relevant Pages

  • Re: A Class of Non-Halting TMs
    ... >> Step, current state, input, and tape position. ... and so it will loop forever" may not be true. ... if you take a nonhalting machine history which eventually ... My method will say steps 7-8 repeat: ...
    (comp.theory)
  • Re: RFC: About error handling - WAS: Re: A remobjects wtf...
    ... Of course in this example (and probably in most cases, as there is some condition that determines whether you need to retry) it could be replaced with a test for a valid BaudRate in 'until'. ... You could be against repeat and while in general with the same argument. ... I would also say that your 'Retry' examples resembles these much more (You wouldn't need 'goto Succeeded', would you?): ... 'break') applies to this 'fake' loop, ...
    (borland.public.delphi.non-technical)
  • Re: Using variables
    ... This code mostly avoids stack gymnastics by using the pseudoregisters p and q for the addresses, representing data whose flow isn't LIFO. ... We could use v-v-dot as a factor, but here we can save some address arithmetic in the outer loop by noting that if p contains the address of a matrix, after iterating it points to the next row of the matrix automatically. ... they use LSE's beloved universal loop word: "repeat". ... you sometimes need to break out of an "iterate" loop prematurely. ...
    (comp.lang.forth)
  • Re: Feedback on Until recipe
    ... I can put it on the python cookbook. ... "while Until" is supposed to remind us of a *repeat* loop? ... Secondly it isn't really what you want because the condition is still being evaluated before the loop body is executed, when the idea of a repeat loop is to terminate the loop after at least one iteration once the condition is true. ...
    (comp.lang.python)
  • Re: Need to loop a mp3 file.
    ... > will loop a mp3 music file? ... lacks a track repeat funcion, the audio editor goldwave ...
    (rec.audio.misc)