I need a global counter, starting from 0, 1, 2, 3, .....
I kind of understand that this \"impure\" code should be separately implemented... I am just starting to understand
What you can look into is state monad. This is a general purpose monad which can be used to manage state. In your case the counter is just a state that you want to maintain.