Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: recursive concept


From   "David Harrison" <[email protected]>
To   <[email protected]>
Subject   st: RE: recursive concept
Date   Tue, 1 Feb 2005 09:11:45 -0000

You can kind of recurse in Stata, as you can call a program from within itself. As you cannot actually define functions, this is not quite as straightforward (or useful?) as in R, but here is my attempt at a recursive factorial:

prog fct, rclass
    args x
    confirm number `x'
    global ans `x'
    _fct `x'
    return scalar fct=$ans
end

prog _fct
    args x
    if `x'<=1 {
        di as result $ans
    }
    else {
        global ans=$ans*`--x'
        _fct `x'
    }
end

David
[email protected]

-----Original Message-----
From: Tony YANG [mailto:[email protected]]
Sent: 31 January 2005 21:42
To: [email protected]
Subject: st: recursive concept


Dear Listers;

I am trying to do something by using the recursive concepts, and I am
not sure if there is some build-in command in Stata to realize it,
since in R, the build-in command Recall can work for it.

e.g.
fact<-function(x) {
   if (x<=1) x else x*Recall(x-1)
}
fact(6)

Any suggestions would be greatly appreciated. Thanks in advance.

Best,
Tony
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index