Skip to content
Prev 165151 / 398506 Next

understanding recursive functions

On 18-Dec-08 22:33:28, Jeffrey Horner wrote:
The following fixes it even more simply (using the same principles):

  q <- function(x,h){
    if (x < 2) {u <- x+1; return(q(u))} else return(x)
  }

Note that "<<-" is not necessary.

Just to test the method more thoroughly:

  q <- function(x,h){
    if (x < 2) {u <- x+h; return(q(u,h))} else return(x)
  }

  q(0,0.3)
# [1] 2.1

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Dec-08                                       Time: 22:51:41
------------------------------ XFMail ------------------------------