R: Lazy Evaluation of Function

In R there is a programming strategy which allows a symbol to evaluated. Here symbol can be defined in a function which will be evaluated only when needed. This strategy of programming is known as Lazy Evaluation.

In R the expression is not evaluated when it is not used. In order to allow for lazy evaluation , R uses a special data structure which is used to store the expression until you look at it then evaluates it.

It creates an infinite data structure without any infinite loop. It's inherently includes iteration in its data structure.

Example

fibonacci<- function(n)
{
    if (n==0)
        return(0)
    if (n==1)
        return(1)
    return(fibonacci(n-1) + fibonacci(n-2))
}

fibonacci(6)

Output: [1] 8