Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Repetition cycles
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



Report this thread to moderator Post Follow-up to this message
Old Post
rsm
04-25-05 01:58 PM


Re: Repetition cycles
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 language
s
> 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))


Report this thread to moderator Post Follow-up to this message
Old Post
Djamé
04-25-05 08:58 PM


Re: Repetition cycles
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


Report this thread to moderator Post Follow-up to this message
Old Post
publiustemp-googlegroups@yahoo.com
04-26-05 09:00 PM


Re: Repetition cycles
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 language
s
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Bart Demoen
04-27-05 01:57 AM


Re: Repetition cycles
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.

Report this thread to moderator Post Follow-up to this message
Old Post
A.L.
04-27-05 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:26 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.