Home > Archive > Prolog > April 2005 > Repetition cycles
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]
|
|
|
| I have a lot of experience with languages such as C or Pascal. I am
beginning to learn prolog (SWI Prolog) and I have a doubt: other languages
have several repetition structures like Repeat, While, For and prolog
haven't. How can we implement by example a For cycle with prolog?
Thank you for your help
Rui Silva
| |
|
| rsm a écrit :
> I have a lot of experience with languages such as C or Pascal. I am
> beginning to learn prolog (SWI Prolog) and I have a doubt: other languages
> have several repetition structures like Repeat, While, For and prolog
> haven't. How can we implement by example a For cycle with prolog?
>
> Thank you for your help
>
> Rui Silva
>
>
that's not the usual way to think about prolog (think in term of recursion)
but one could implement a for loop like this
for(I,J,Statement):-
( I <= J ->
call(Statement),
for(I+1,J,Statement) %line 4
;
true
).
maybe one could replace line 4 by
(New_I is I +1 , for(New_I,J,Statement))
| |
| publiustemp-googlegroups@yahoo.com 2005-04-26, 4:00 pm |
| rsm wrote:
> I have a lot of experience with languages such as C or Pascal. I am
> beginning to learn prolog (SWI Prolog) and I have a doubt: other
languages
> have several repetition structures like Repeat, While, For and prolog
> haven't. How can we implement by example a For cycle with prolog?
Most Prolog implementations have a repeat/0 predicate that you'll want
to take a look at. However, as a general rule, you should carefully
consider whether or not such things are really what you want. Doing
things in a procedural fashion isn't really what Prolog is geared
towards.
Cheers,
Ovid
| |
| Bart Demoen 2005-04-26, 8:57 pm |
| rsm wrote:
> I have a lot of experience with languages such as C or Pascal. I am
> beginning to learn prolog (SWI Prolog) and I have a doubt: other languages
> have several repetition structures like Repeat, While, For and prolog
> haven't. How can we implement by example a For cycle with prolog?
>
> Thank you for your help
>
> Rui Silva
>
>
I my opinion, once more, all replies you got have missed some essence:-)
Let me paraphrase on a known proverb: if all you have is the induction
domain of the natural numbers, everything looks like a for loop.
Very often - most often if you are into symbolic computing - the domain
you do induction on is not the natural numbers but the ... euh,
inductive domain of symbols, like lists, or trees and sometimes even the
natural numbers. You then use two important "facts"
- any program (say in Pascal, C, Java, ...) using a for
(while/repeat) loop can be transformed to a recursive program
not using such a construct
- the natural loop going over an inductively defined type
(list, tree, ... and also the natural numbers) is done by
recursion
And then of course, Prolog is a single assigment language. You can't say
(straithforwardly) things like
for every element of a given list, add its value to another list
So forget about repeat/0 (unless you have side-effects) or for/3 (unless
there are no bindings produced - in which case you could as well not do
the loop) - at least for a while until you are used to programming with
definitions.
There are Prolog systems (ECLiPSe for instance) that offer a special
syntax for going over inductively defined data, but using them when you
come straight from Pascal, might obscure some of the essence of logic
programming.
Cheers
Bart Demoen
| |
|
| On Mon, 25 Apr 2005 11:39:25 +0100, "rsm" <msmr@netcabo.pt> wrote:
>I have a lot of experience with languages such as C or Pascal. I am
>beginning to learn prolog (SWI Prolog) and I have a doubt: other languages
>have several repetition structures like Repeat, While, For and prolog
>haven't.
Because Prolog is not Pascal. Although it is possible to write Pascal
program in Prolog, it is much easier and more efficient to do this
in... right, Pascal...
A.L.
|
|
|
|
|