modifying the foreach variable from within the foreach body

Discussions related to Visual Prolog
mdosnov2016
VIP Member
Posts: 73
Joined: 19 Nov 2016 7:58

modifying the foreach variable from within the foreach body

Unread post by mdosnov2016 »

Hello,
is there any way to modify the foreach variable from within the foreach body?
e.g.

Code: Select all

foreach Y = std::fromTo(1, 100) do  <command to change the value of Y>
I need to re-start the loop with different begin and end values, and I cannot
implement this without recursion.
Michael
PrologUser
Paul Cerkez
VIP Member
Posts: 94
Joined: 6 Mar 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Paul Cerkez »

I don't remember if it exists in VIP or not but how about a "while" loop instead of a foreach?

pseudo code:

Code: Select all

Y = 1 While Y < 100,    <do steps>    < change value of Y (either increment by 1 or set to 100)> End While.
in pseudo VIP:

Code: Select all

... Y=1, MyWhileLoop(Y, Omega), % Omega is the upper limit) ....   MyWhileLoop(Y, Omega):-  %This is your 'while' loop    Y<Omega, !,   <do steps>     MyWhileLoop1(Y, Omega, NewOmega),     ... MyWhileLoop(Y, Omega).   MyWhileLoop1(Y, Omega, NewOmega):-  % this is the looping 'control' (value of Y)      <do condition check for terminating>       !,       NewOmega = Y,       MyWhileLoop(Y, NewOmega ).   MyWhileLoop1(Y, Omega, NewOmega):-       NewY = Y +1,       NewOmege = Omega,       MyWhileLoop(NewY, NewOmega).
AI Rules!
P.
mdosnov2016
VIP Member
Posts: 73
Joined: 19 Nov 2016 7:58

Re: modifying the foreach variable from within the foreach body

Unread post by mdosnov2016 »

I know how to do recursion, thank you.
The problem with recursion is that it eats up the whole of my stack.
The problem is not with tail-recursive predicates, but with others
that call each other thousands of times...
b.r. Michael
PrologUser
Paul Cerkez
VIP Member
Posts: 94
Joined: 6 Mar 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Paul Cerkez »

sorry, missed the implied cannot use recursion.

Deferring to Thomas.
AI Rules!
P.
Paul Cerkez
VIP Member
Posts: 94
Joined: 6 Mar 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Paul Cerkez »

Michael,

dumb question. I'm confused.

Are you trying to restart the same foreach() loop from within the foreach() loop but with new start and stop numbers based on a action/decision within that loop?

<clause>:-
... steps ...
foreach(), %change the iterations of this loop and restart just the loop from within
end foreach.
... more steps ...
<end clause>.

OR,
are you just trying to stop the loop 'early' once a condition is met?

I get both possibilities when reading your initial post.
AI Rules!
P.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1177
Joined: 28 Feb 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Thomas Linder Puls »

What you are asking for is impossible. "Green" variables are not modifiable.

Furthermore std::fromTo(...) nondeterministically returns a sequence of numbers, it does not look at the current value of Y to find the next value.
Regards Thomas Linder Puls
PDC
Paul Cerkez
VIP Member
Posts: 94
Joined: 6 Mar 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Paul Cerkez »

Thomas,
The std::fromTo() 'sets' the next value of 'Y' from the sequence each time though, correct?
AI Rules!
P.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1177
Joined: 28 Feb 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Thomas Linder Puls »

Yes, that is what I mean by "nondeterministically returns a sequence...". I.e. it returns one value at the time nondeterministically.
Regards Thomas Linder Puls
PDC
Post Reply