Re: Loop for printing numbers in Prolog
- From: Stephan Lukits <stephan.lukits@xxxxxxxxxxxxxxxx>
- Date: Fri, 29 Feb 2008 15:31:36 +0100
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
.
- References:
- Loop for printing numbers in Prolog
- From: George
- Loop for printing numbers in Prolog
- Prev by Date: Loop for printing numbers in Prolog
- Previous by thread: Loop for printing numbers in Prolog
- Index(es):
Relevant Pages
|
|