I think you actually want NestWhileList
. The reason is that FoldList
"folds" successive elements of a list into the calculation in a fixed order, as this minimal example shows:
FoldList[0.9*#1 + #2 & , 0., RandomVariate[NormalDistribution[0,1],{100}]]
But you seem to need to access the data
set at indices that might not be successive and seem only to be determined as part of the longer calculation.
You can also build in the condition to break the loop in directly to this construct, without having 1970s-BASIC-style Goto
and Label
expressions.
I am not going to rewrite your whole Module
for you, as I think it will be a useful learning exercise to do it yourself. Remember you will need to use a pure function in the first argument. Here are some hints to get you started:
First, think of things in terms of a state vector and work out how that state vector gets updated. In your case, you have four elements: x[n]
, y[n]
, z[n]
, and V[n]
, so you should be setting up the NestWhileList
to take a four-element vector and output a four-element vector.
Second, this piece
V[n + 1] = rk[x[n], y[n], z[n], V[n]];
vs = V[n].V[n + 1];
V[n + 1] = If[vs < 0, (-1)*V[n + 1], V[n + 1]];
Can be rewritten as:
V[n+1]= With[{tmp=rk[x[n], y[n], z[n], V[n]]}, Sign[V[n]*tmp]*tmp]
Subscript
constructs, which just make it harder to read on a web page. The short answer to your question is "yes, you can use these other functions likeFoldList
, and yes, you should". However, without knowing whatdata
andfunc1
are, it is a bit hard to work out code to actually implement what you want to do. Similarly, how does one geta[0]
etc? And what arem
andn
? Please post a minimal working example so that people can help you. – Verbeia♦ Jan 9 at 22:37rk
. If you have a separate function for it, you probably shouldn't have it as a local variable inside theModule
. May I also point out that completely changing the variable names half an hour after posting the original question makes it quite difficult for people who are trying to answer the question to keep track. I would also recommend against using single capital letters as variable names (V
etc) as they are often reserved for use as system symbols. – Verbeia♦ Jan 9 at 23:43rk
, what isFa
, and how to you callcurve
? – Mr.Wizard♦ Jan 10 at 9:41