For Programmers: Free Programming Magazines  


Home > Archive > Prolog > March 2008 > Loop for printing numbers in Prolog









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Loop for printing numbers in Prolog
George

2008-02-29, 8:21 am

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 know it can be easily done with a recursive algorithm, but I was just
wondering whether looping is also an option.

Thanks,
George
Stephan Lukits

2008-02-29, 7:14 pm

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
Matthew Huntbach

2008-03-03, 4:39 am



On Fri, 29 Feb 2008, George wrote:

> 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).


If you want a variable to be given a different value each time
computation backtracks, then you have to provide multiple clauses
somewhere, so that backtracking goes back and tries an alternative clause
to that used last time. You do not provide any multiple clauses in
the code above, hence there can be no choice points left anywhere,
hence no change of variable value through backtracking, which is
what you seem to be trying to use.

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


If what you want can be done through recursion, then why try and do
it through looping? Prolog is a programming language where recursion
is the natural pattern to use, it should be used in most cases where
loops would be used in an imperative language. In Prolog, recursion is
the plain straightforward way of programming which novices should master
first, "loops" which involve more complex aspects of Prolog and aren't
really the equivalent of imperative programming loops anyway, should be
regarded as an advanced feature which shouldn't be touched until you are
throughly familiar with the basics.

The problem seens to be that while a small number of people find
recursive thinking natural, most people seem to find the abstraction
step it requires to be a difficult one to take, and therefore will
go to absurd lengths to try and avoid using recursion. This is what I
have found when teaching programming in an imperative language - even
when the students have been taught carefully about recursion and how
to use it and think using recursion, if they are presented with a problem
to solve which can easily be done using recursion, 90% of them will instead
pick a more complex iterative solution.

If your real problem here is that you are afraid of recursion, just
practice, practice, practice, until you are not and it comes naturally.

Matthew Huntbach
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com