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 » 16 Jan 2018 17:12

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: 388
Joined: 6 Mar 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Paul Cerkez » 16 Jan 2018 18:10

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 » 17 Jan 2018 8:33

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: 388
Joined: 6 Mar 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Paul Cerkez » 17 Jan 2018 12:43

sorry, missed the implied cannot use recursion.

Deferring to Thomas.
AI Rules!
P.

Paul Cerkez
VIP Member
Posts: 388
Joined: 6 Mar 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Paul Cerkez » 17 Jan 2018 13:21

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: 2447
Joined: 28 Feb 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Thomas Linder Puls » 17 Jan 2018 15:38

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: 388
Joined: 6 Mar 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Paul Cerkez » 17 Jan 2018 17:08

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: 2447
Joined: 28 Feb 2000 0:01

Re: modifying the foreach variable from within the foreach body

Unread post by Thomas Linder Puls » 18 Jan 2018 9:42

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