Not able to apply Scan in place of For loop [closed]

I have been advised to avoid procedural loops and use other functions like Scan,NestList, FoldList, NestWhileList, Inner and Outer. My worry is that if I have a need in which certain critical parameters have to be calculated at the end of each loop and then passed on to the next iteration which in simpler terms would mean that certain outputs of an iteration have to be passed as inputs for the very next iteration, then how to go about that.


list1 = Scan[someFunction,someList]

Suppose someFunction has certain input parameters a; b; with some initial values at the beginning which are to be used for the first iteration only.
Let c; d; e; be the output where e is the main output to be stored in list1 while c and d are to be passed as input to a and b respectively in the very next iteration.

Actual Example:

a=1;b=1; (*For first iteration only*)

someFunction[inp_] :=






2 Answers


For your specific example, why not:

FoldList[# + #2^2 + #2^3 &, 1, someList]

It’s not quite clear what is the role of someList in what you’re trying to implement, but if I understand you correctly you can do it with Table:

c=a; d=b;
Table[{c,d,e}=someFunction[c,d,x];e , {x,someList}]



Where exactly c=a; and d=b would have to be added in the code, within the function someFunction or outside.
– Sone
Jun 21 at 12:12