Controlling Environments in R

While I liked your solution very much, I can't use your function in my code directly.

I tried two ways.

First, I defined tick <- counter() in the base environment, for the first implementation, it displays correct result. But for the next ones, the counter does not get reset, and it continues to get incremented recursively.

So, I tried to define inside f. But that led to:

Error in tick() : could not find function "tick"

So, I used tick <<- counter(), and it works as I would have liked. But in that case, isn't it the same as what I did earlier?

The problem is the idea of closure is new to me. Can you provide me some reference? Then, possibly I can modify your code to suit my problem.