Page 1 of 1

### modifying the foreach variable from within the foreach body

Posted: 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

### Re: modifying the foreach variable from within the foreach body

Posted: 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). ``````

### Re: modifying the foreach variable from within the foreach body

Posted: 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

### Re: modifying the foreach variable from within the foreach body

Posted: 17 Jan 2018 12:43
sorry, missed the implied cannot use recursion.

Deferring to Thomas.

### Re: modifying the foreach variable from within the foreach body

Posted: 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?

### Re: modifying the foreach variable from within the foreach body

Posted: 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.

### Re: modifying the foreach variable from within the foreach body

Posted: 17 Jan 2018 17:08
Thomas,
The std::fromTo() 'sets' the next value of 'Y' from the sequence each time though, correct?

### Re: modifying the foreach variable from within the foreach body

Posted: 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.