In general (e.g. if you don't know what locals does r() contain, or
they vary in different iterations) you can obtain that list with
`:r(macros)'. There are similar lists for scalars and matrices (and
mysterious functions...).
Hence the the solution is:
/// A "do nothing" recursion example; recur calls itself "stopit" times,
// and tries to return something each time in the locals something0,
// something1, something2, .., but only "something0" shows up in the
return list
cap prog drop recr
prog recr, rclass
args count stopit
if `count' == `stopit' {
return local something`count' = -99
}
else {
recr `=`count'+1' `stopit'
foreach mac in `:r(macros)' {
return local `mac' `=r(`mac')'
}
return local something`count' = uniform()
}
end
// Start count at 0, run 5 times
recr 0 5
ret list
. ret list
macros:
r(something0) : ".9477426449302584"
r(something2) : ".5644091702997685"
r(something4) : ".8414094401523471"
r(something5) : "-99"
r(something3) : ".2110076593235135"
r(something1) : ".2648020917549729"
Best regards, Sergiy Radyakin
On Mon, Oct 20, 2008 at 2:41 PM, Jeph Herrin <[email protected]> wrote:
> Mike,
>
> Interesting problem.
>
> Without actually trying it myself, I'd think that
> each call to -recr- inside the program would need
> to be followed by a loop which captures the return
> list and then -return-s it. Something like:
>
> else {
> recr `k' `stopit'
> forv r=1/`count' {
> return local something`r' r(something`r')
> }
> local k = `count' +1
> return local something`count' = uniform()
> }
>
> Hth,
> Jeph
>
>
>
> Mike Lacy wrote:
>>
>> I've been experimenting with implementing recursive calls to a Stata
>> program, and in particular, getting the program to store something in the
>> return list at each call, so that at the end of program, the return list
>> would contain r(something1) , r(something2), ..., r(somethingNumberOfCalls).
>> I have experienced various difficulties in not clobbering the return list.
>> Here's a simple example to demonstrate one of the problems I have had:
>>
>> /// A "do nothing" recursion example; recur calls itself "stopit" times,
>> // and tries to return something each time in the locals something0,
>> // something1, something2, .., but only "something0" shows up in the
>> return list
>> cap prog drop recr
>> prog recr, rclass
>> args count stopit
>> if `count' == `stopit' {
>> return local something`count' = -99
>> }
>> else {
>> return local something`count' = uniform()
>> local k = `count' +1
>> recr `k' `stopit'
>> }
>> end
>> // Start count at 0, run 5 times
>> recr 0 5
>> ret list
>>
>> macros:
>> r(something0) : ".2769154107663781"
>>
>> How could this be changed so as to accumulate something in the return list
>> at each recursive call?
>>
>> Thanks,
>>
>>
>>
>>
>> =-=-=-==-=-=-==-=-=
>> Mike Lacy
>> Director of Graduate Studies, Department of Sociology
>> Colorado State University, Fort Collins CO 80523 USA
>> [email protected] (970) 491-6721 (voice)
>> http://www.colostate.edu/Depts/Sociology/graduate/graduate.html
>>
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/statalist/faq
>> * http://www.ats.ucla.edu/stat/stata/
>>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/