# 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.

ForEg:

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_] :=

(
e=inp+a+b;
c=e^2;
d=e^3;
e
);

someList=Range[0,0.1,0.0001];

list1=Scan[someFunction,someList];

• What is someList? I think you are looking for either FoldList or NestList but I cannot tell from your description which one. Possibly also Fold or Nest combined with Sow and Reap. – Mr.Wizard Jun 21 '16 at 11:47
• @Mr.Wizard I meant an arbitrary function called someFunction is applied to an arbitrary list calledsomeList – Sone Jun 21 '16 at 11:50
• It would help me if you included an actual For loop you wish to refactor. My confusion comes from the fact that you say someFunction has the "input variables" (usually called parameters if I understand) a and b, and these are filled from the output c and d of the previous iteration. This sounds like Nest or NestList. However if someFunction has additional parameters that should be filled from someList then I think you want Fold or FoldList. – Mr.Wizard Jun 21 '16 at 11:52
• Scan applies a function to each element of a list BUT does not return anything (well it returns Null); it is intended to be used on functions that operate through side effects. If you're expecting a list of results from applying a function to a list of inputs then you want Map not Scan. – Ymareth Jun 21 '16 at 12:45
• in your "Actual Example" a,b never change. Your question implies you have a working For loop solution, you should actually show that. – george2079 Jun 21 '16 at 13:31

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;

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