Recursion, environment

I have a task to write four simple funtions in R . One of these functions is a master function with only one argument, which tells how many times the other three functions should be executed. These functions should be executed at random and they should print their own names. The problem is that respectively executed functions should be defined in different environments. To define these functions i should use only global environment and one additionally defined env. How should i apply recursion to this problem?

Typical use

master( 20)

[1] "f1"
[1] "f2"
[1] "f3"
[1] "f1"
[1] "f1"
[1] "f2"
[1] "f3"
[1] "f2"
[1] "f1"
[1] "f1"
[1] "f2"
[1] "f3"
[1] "f3"
[1] "f1"
[1] "f2"
[1] "f1"
[1] "f2"
[1] "f1"
[1] "f2"
[1] "f1"

Hi @Kopasasd! Welcome!

Is your question related to work you are doing for a course or training? If so, please take a look at this FAQ and make sure you're following our policies for such questions:

Either way, I think it will help if you can show some of the code you've tried so far. There are a lot of different things going on in your question, and people will be able to help best if they can see exactly where you're getting stuck.

I have done this so far..., but i don't have any idea how to apply recursion here, and i am not sure if i am doing this task correctly.

new_env<-env()

master<-function(x){
  f1<-function(){
    print('f1')
   }
    master2 <- function(){
        f2<-function(){
        print('f2')
        
      }
      
      los = ceiling(runif(1)*3)
      if (los == 1){
        f1()
      }
      else if (los ==2){
        f2()
      }
      else {
        new_env$f3()
      }
    }  
  
  
  
  
  for (i in 1:x){
      master2()
    }
  }




new_env$f3<-function(){
  print('f3')
  
}
master(20)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.