1
$\begingroup$

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];   
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Mr.Wizard Jun 21 '16 at 11:47
  • $\begingroup$ @Mr.Wizard I meant an arbitrary function called someFunction is applied to an arbitrary list calledsomeList $\endgroup$ – Sone Jun 21 '16 at 11:50
  • 2
    $\begingroup$ 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. $\endgroup$ – Mr.Wizard Jun 21 '16 at 11:52
  • 2
    $\begingroup$ 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. $\endgroup$ – Ymareth Jun 21 '16 at 12:45
  • 1
    $\begingroup$ in your "Actual Example" a,b never change. Your question implies you have a working For loop solution, you should actually show that. $\endgroup$ – george2079 Jun 21 '16 at 13:31
3
$\begingroup$

For your specific example, why not:

FoldList[# + #2^2 + #2^3 &, 1, someList]
$\endgroup$
1
$\begingroup$

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}]
$\endgroup$
  • $\begingroup$ Where exactly c=a; and d=b would have to be added in the code, within the function someFunction or outside. $\endgroup$ – Sone Jun 21 '16 at 12:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.