[R] Explain how it gets back out?

(Ted Harding) ted.harding at wlandres.net
Tue Jul 12 21:02:50 CEST 2011


On 12-Jul-11 17:18:26, mousy0815 wrote:
> Probability <- function(N, f, m, b, x, t) {
>       #N is the number of lymph nodes
>       #f is the fraction of Dendritic cells (in the correct node) that
have
> the
> antigen
>       #m is the number of time steps
>       #b is the starting position (somewhere in the node or somewhere
in the
> gap
> between nodes. It is a number between 1 and (x+t))
>       #x is the number of time steps it takes to traverse the gap
>       #t is the number of time steps it takes to traverse a node. 
>               A <- 1/N
>               B <- 1-A
>               C <- 1-f
>               D <- (((m+b-1)%%(x+t))+1) 
> 
>               if (b<=t) {########starts inside node
>                       if (m<=(t-b)){return(B + A*(C^m))} # start & end
in first node          
>                       if (D<=t) { # we finish in a node
>                               a <- (B + A*(C^(t-b))) #first node
>                               b <- ((B +
A*(C^t))^(floor((m+b)/(x+t))-1))  # intermediate nodes
> (if
> any)
>                               c <- (B + A*(C^D))  # last node   
>                               d <- (a*b*c)    
>                               return(d)
>                               } else {Probability(N, f, (m-1), b, x,
t)} ## finish in a gap   
>               } else {###### starts outside node
>                       if (m<=(x+t-b)) {return(1)} #also end in the gap
>                       if (D<=t) { #end in a node
>                               b <- ((B + A*(C^t))^(floor((m/(x+t)))))
>                               c <- (B + (A*(C^D)))
>                               d <- (b*c)
>                               return(d)
>                               } else {Probability(N, f, (m-1), b, x,
t)} #outside node
>               }
>       }       
> 
> I have the following code and I know it works, but I need to
> explain what is going on, particularly with the recursion.
> Is it true that "when each call finishes - it will pass a
> quantity back to the next generation above until you return
> to the start of the chain, then outputs the final result."?
> If so, could someone explain it in a bit more clearly? if not,
> how does the recursion work - how does it finally output a
> value?
> --

This is a generic reply, rather than referring to your specific
code above.

The most succinct definition of recursion is in Ted's Dictionary:

*Recursion*
  If you understand *Recursion*, then stop reading now and
  do something else.
  Otherwise, see *Recursion".

(I have found that this goes down well in lectures. It presupposes
however that the reader will eventually catch on, so the definition
is not suitable for the infinitely stupid -- which is perhaps a
realistic assumption in a lecture context).

The really important element in the above definition is the initial
"escape clause" (which, by the above assumption, will eventually
be realised). A proper recursive definition must include something
which will eventually cause it to return a result to level above.

The structure of the process which occurs when a recursive function
is called can be illustrated by a function to compute n! (the factorial
of a a positive integer n):

factorial <- function(n) {
  if(n==0) return(1) else    ## Escape clause
  return( n*factorial(n-1) )
}

So what happens when you call 'factorial(3)' is:

  n==3 so !(n==0) so
  return(3*(
    n==2 so !(n==0) so
      return(2*(
        n==1 so !(n==0) so
          return(1*(
            n==0 so return 1
                 1*(1) = 1
             2*(1) = 2
         3*(2) = 6
  return(6)


Another way of looking at it is that each successive call
opens a "*(" in the expression

  (3*(3-1=2*(2-1=1*(1-1==0 -> 1 | Escape clause activated here
= (3*(2*(1*(1 ...

and then backing up through the levels completes each level
with a ")" and passes up the result, resulting successively in

  (3*(2*(1*(1) ...
= (3*(2*(1*1 ...
= (3*(2*(1 ...

  (3*(2*(1) ...
= (3*(2*1
= (3*(2 ...

  (3*(2) ...
= (3*2 ...
= (6 ...

  (6)
= 6

Hoping this helps!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 12-Jul-11                                       Time: 20:02:47
------------------------------ XFMail ------------------------------



More information about the R-help mailing list